* [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments
@ 2022-05-05 10:20 Andreas Rheinhardt
2022-05-05 10:21 ` [FFmpeg-devel] [PATCH 2/3] lavf/matroskaenc: enable PGS merge auto bsf Andreas Rheinhardt
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Andreas Rheinhardt @ 2022-05-05 10:20 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: John Stebbins, Andreas Rheinhardt
From: John Stebbins <jstebbins@jetheaddev.com>
Required to remux m2ts to mkv
Minor changes and porting to FFBitStreamFilter done by the committer.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
Changelog | 1 +
doc/bitstream_filters.texi | 8 ++
libavcodec/Makefile | 1 +
libavcodec/bitstream_filters.c | 1 +
libavcodec/pgs_frame_merge_bsf.c | 191 +++++++++++++++++++++++++++++++
libavcodec/version.h | 2 +-
6 files changed, 203 insertions(+), 1 deletion(-)
create mode 100644 libavcodec/pgs_frame_merge_bsf.c
diff --git a/Changelog b/Changelog
index 4d467eb741..1bae64075e 100644
--- a/Changelog
+++ b/Changelog
@@ -14,6 +14,7 @@ version 5.1:
- colormap video filter
- colorchart video source filter
- blurdetect filter
+- PGS subtitle frame merge bitstream filter
version 5.0:
diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index fc2c71fc12..50c95f035d 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -695,6 +695,14 @@ for NTSC frame rate using the @option{frame_rate} option.
ffmpeg -f lavfi -i sine=r=48000:d=1 -c pcm_s16le -bsf pcm_rechunk=r=30000/1001 -f framecrc -
@end example
+@section pgs_frame_merge
+
+Merge a sequence of PGS Subtitle segments ending with an "end of display set"
+segment into a single packet.
+
+This is required by some containers that support PGS subtitles
+(muxer @code{matroska}).
+
@section prores_metadata
Modify color property metadata embedded in prores stream.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index cfaa6f196a..3c0ff2dc58 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1195,6 +1195,7 @@ OBJS-$(CONFIG_NOISE_BSF) += noise_bsf.o
OBJS-$(CONFIG_NULL_BSF) += null_bsf.o
OBJS-$(CONFIG_OPUS_METADATA_BSF) += opus_metadata_bsf.o
OBJS-$(CONFIG_PCM_RECHUNK_BSF) += pcm_rechunk_bsf.o
+OBJS-$(CONFIG_PGS_FRAME_MERGE_BSF) += pgs_frame_merge_bsf.o
OBJS-$(CONFIG_PRORES_METADATA_BSF) += prores_metadata_bsf.o
OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o av1_parse.o
OBJS-$(CONFIG_SETTS_BSF) += setts_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index f117bc0e17..444423ae93 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -52,6 +52,7 @@ extern const FFBitStreamFilter ff_noise_bsf;
extern const FFBitStreamFilter ff_null_bsf;
extern const FFBitStreamFilter ff_opus_metadata_bsf;
extern const FFBitStreamFilter ff_pcm_rechunk_bsf;
+extern const FFBitStreamFilter ff_pgs_frame_merge_bsf;
extern const FFBitStreamFilter ff_prores_metadata_bsf;
extern const FFBitStreamFilter ff_remove_extradata_bsf;
extern const FFBitStreamFilter ff_setts_bsf;
diff --git a/libavcodec/pgs_frame_merge_bsf.c b/libavcodec/pgs_frame_merge_bsf.c
new file mode 100644
index 0000000000..bcd8945335
--- /dev/null
+++ b/libavcodec/pgs_frame_merge_bsf.c
@@ -0,0 +1,191 @@
+/*
+ * Copyright (c) 2020 John Stebbins <jstebbins.hb@gmail.com>
+ *
+ * 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 merges PGS subtitle packets containing incomplete
+ * set of segments into a single packet
+ *
+ * Packets already containing a complete set of segments will be passed through
+ * unchanged.
+ */
+
+#include "libavutil/attributes.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/log.h"
+#include "bsf.h"
+#include "bsf_internal.h"
+
+enum PGSSegmentType {
+ PALETTE_SEGMENT = 0x14,
+ OBJECT_SEGMENT = 0x15,
+ PRESENTATION_SEGMENT = 0x16,
+ WINDOW_SEGMENT = 0x17,
+ END_DISPLAY_SET_SEGMENT = 0x80,
+};
+
+typedef struct PGSMergeContext {
+ AVPacket *buffer_pkt, *in;
+ int presentation_found;
+ int pkt_flags;
+} PGSMergeContext;
+
+static av_cold void frame_merge_flush(AVBSFContext *bsf)
+{
+ PGSMergeContext *ctx = bsf->priv_data;
+
+ ctx->presentation_found = ctx->pkt_flags = 0;
+ av_packet_unref(ctx->in);
+ av_packet_unref(ctx->buffer_pkt);
+}
+
+static int frame_merge_output(PGSMergeContext *ctx, AVPacket *dst, AVPacket *src)
+{
+ if (!ctx->presentation_found)
+ ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
+ ctx->presentation_found = 0;
+ src->flags |= ctx->pkt_flags;
+ ctx->pkt_flags = 0;
+ av_packet_move_ref(dst, src);
+ return 0;
+}
+
+static int frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
+{
+ PGSMergeContext *ctx = bsf->priv_data;
+ AVPacket *in = ctx->in, *pkt = ctx->buffer_pkt;
+ int ret, size, pos, display = 0, presentation = 0;
+ unsigned int i;
+
+ if (!in->data) {
+ ret = ff_bsf_get_packet_ref(bsf, in);
+ if (ret == AVERROR_EOF && pkt->data) {
+ // Output remaining data
+ ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
+ return frame_merge_output(ctx, out, pkt);
+ }
+ if (ret < 0)
+ return ret;
+ }
+ if (!in->size) {
+ av_packet_unref(in);
+ return AVERROR(EAGAIN);
+ }
+ in->flags &= ~AV_PKT_FLAG_KEY; // Will be detected in the stream
+
+ // Validate packet data and find display_end segment
+ size = in->size;
+ i = 0;
+ while (i + 3 <= in->size) {
+ uint8_t segment_type = in->data[i];
+ int segment_len = AV_RB16(in->data + i + 1) + 3;
+
+ if (i + segment_len > in->size)
+ break; // Invalid, segments can't span packets
+ if (segment_type == PRESENTATION_SEGMENT && ctx->presentation_found)
+ break; // Invalid, there can be only one
+ if (segment_type == PRESENTATION_SEGMENT) {
+ uint8_t state;
+ if (segment_len < 11)
+ break; // Invalid presentation segment length
+ ctx->presentation_found = presentation = 1;
+ state = in->data[i + 10] & 0xc0;
+ if (state)
+ ctx->pkt_flags |= AV_PKT_FLAG_KEY;
+ else
+ ctx->pkt_flags &= ~AV_PKT_FLAG_KEY;
+ }
+ i += segment_len;
+ if (segment_type == END_DISPLAY_SET_SEGMENT) {
+ size = i;
+ display = 1;
+ break;
+ }
+ }
+ if (display && pkt->size == 0 && size == in->size) // passthrough
+ return frame_merge_output(ctx, out, in);
+ if (!display && i != in->size) {
+ av_log(bsf, AV_LOG_WARNING, "Failed to parse PGS segments.\n");
+ // force output what we have
+ size = in->size;
+ display = 1;
+ ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
+ }
+
+ if (presentation) {
+ ret = av_packet_copy_props(pkt, in);
+ if (ret < 0)
+ goto fail;
+ }
+ pos = pkt->size;
+ ret = av_grow_packet(pkt, size);
+ if (ret < 0)
+ goto fail;
+ memcpy(pkt->data + pos, in->data, size);
+
+ if (size == in->size)
+ av_packet_unref(in);
+ else {
+ in->data += size;
+ in->size -= size;
+ }
+
+ if (display)
+ return frame_merge_output(ctx, out, pkt);
+ return AVERROR(EAGAIN);
+
+fail:
+ frame_merge_flush(bsf);
+ return ret;
+}
+
+static av_cold int frame_merge_init(AVBSFContext *bsf)
+{
+ PGSMergeContext *ctx = bsf->priv_data;
+
+ ctx->in = av_packet_alloc();
+ ctx->buffer_pkt = av_packet_alloc();
+ if (!ctx->in || !ctx->buffer_pkt)
+ return AVERROR(ENOMEM);
+
+ return 0;
+}
+
+static av_cold void frame_merge_close(AVBSFContext *bsf)
+{
+ PGSMergeContext *ctx = bsf->priv_data;
+
+ av_packet_free(&ctx->in);
+ av_packet_free(&ctx->buffer_pkt);
+}
+
+static const enum AVCodecID frame_merge_codec_ids[] = {
+ AV_CODEC_ID_HDMV_PGS_SUBTITLE, AV_CODEC_ID_NONE,
+};
+
+const FFBitStreamFilter ff_pgs_frame_merge_bsf = {
+ .p.name = "pgs_frame_merge",
+ .p.codec_ids = frame_merge_codec_ids,
+ .priv_data_size = sizeof(PGSMergeContext),
+ .init = frame_merge_init,
+ .flush = frame_merge_flush,
+ .close = frame_merge_close,
+ .filter = frame_merge_filter,
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 735c8b813c..87b7284a95 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
#include "version_major.h"
-#define LIBAVCODEC_VERSION_MINOR 27
+#define LIBAVCODEC_VERSION_MINOR 28
#define LIBAVCODEC_VERSION_MICRO 100
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
--
2.32.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".
^ permalink raw reply [flat|nested] 4+ messages in thread
* [FFmpeg-devel] [PATCH 2/3] lavf/matroskaenc: enable PGS merge auto bsf
2022-05-05 10:20 [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments Andreas Rheinhardt
@ 2022-05-05 10:21 ` Andreas Rheinhardt
2022-05-05 10:21 ` [FFmpeg-devel] [PATCH 3/3] fate/matroska: Add tests for muxing PGS into Matroska Andreas Rheinhardt
2022-05-09 19:29 ` [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments Andreas Rheinhardt
2 siblings, 0 replies; 4+ messages in thread
From: Andreas Rheinhardt @ 2022-05-05 10:21 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: John Stebbins, Andreas Rheinhardt
From: John Stebbins <jstebbins@jetheaddev.com>
PGS segments must be merged to one packet for muxing to mkv
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
configure | 2 +-
libavformat/matroskaenc.c | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/configure b/configure
index 196873c4aa..4d2f4d9112 100755
--- a/configure
+++ b/configure
@@ -3435,7 +3435,7 @@ latm_muxer_select="aac_adtstoasc_bsf mpeg4audio"
matroska_audio_muxer_select="matroska_muxer"
matroska_demuxer_select="riffdec"
matroska_demuxer_suggest="bzlib zlib"
-matroska_muxer_select="mpeg4audio riffenc vp9_superframe_bsf aac_adtstoasc_bsf"
+matroska_muxer_select="mpeg4audio riffenc aac_adtstoasc_bsf pgs_frame_merge_bsf vp9_superframe_bsf"
mlp_demuxer_select="mlp_parser"
mmf_muxer_select="riffenc"
mov_demuxer_select="iso_media riffdec"
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 3b8ca11f28..fe296197b1 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -3172,6 +3172,9 @@ static int mkv_check_bitstream(AVFormatContext *s, AVStream *st,
ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
} else if (st->codecpar->codec_id == AV_CODEC_ID_VP9) {
ret = ff_stream_add_bitstream_filter(st, "vp9_superframe", NULL);
+ } else if (CONFIG_MATROSKA_MUXER &&
+ st->codecpar->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE) {
+ ret = ff_stream_add_bitstream_filter(st, "pgs_frame_merge", NULL);
}
return ret;
--
2.32.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".
^ permalink raw reply [flat|nested] 4+ messages in thread
* [FFmpeg-devel] [PATCH 3/3] fate/matroska: Add tests for muxing PGS into Matroska
2022-05-05 10:20 [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments Andreas Rheinhardt
2022-05-05 10:21 ` [FFmpeg-devel] [PATCH 2/3] lavf/matroskaenc: enable PGS merge auto bsf Andreas Rheinhardt
@ 2022-05-05 10:21 ` Andreas Rheinhardt
2022-05-09 19:29 ` [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments Andreas Rheinhardt
2 siblings, 0 replies; 4+ messages in thread
From: Andreas Rheinhardt @ 2022-05-05 10:21 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Andreas Rheinhardt
They test the new pgs_frame_merge BSF.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
tests/fate/matroska.mak | 8 ++++++++
tests/ref/fate/matroska-pgs-remux | 7 +++++++
tests/ref/fate/matroska-pgs-remux-durations | 7 +++++++
3 files changed, 22 insertions(+)
create mode 100644 tests/ref/fate/matroska-pgs-remux
create mode 100644 tests/ref/fate/matroska-pgs-remux-durations
diff --git a/tests/fate/matroska.mak b/tests/fate/matroska.mak
index c9d88975ca..7baf85f29e 100644
--- a/tests/fate/matroska.mak
+++ b/tests/fate/matroska.mak
@@ -163,6 +163,14 @@ FATE_MATROSKA_FFMPEG_FFPROBE-$(call REMUX, MATROSKA, MPEGTS_DEMUXER AC3_DECODER)
+= fate-matroska-mpegts-remux
fate-matroska-mpegts-remux: CMD = transcode mpegts $(TARGET_SAMPLES)/mpegts/pmtchange.ts matroska "-map 0:2 -map 0:2 -c copy -disposition:a:1 -visual_impaired+hearing_impaired -default_mode infer" "-map 0 -c copy" "" "-show_entries stream_disposition:stream=index"
+FATE_MATROSKA-$(call REMUX, MATROSKA, SUP_DEMUXER) += fate-matroska-pgs-remux
+fate-matroska-pgs-remux: CMD = transcode sup $(TARGET_SAMPLES)/sub/pgs_sub.sup matroska "-copyts -c:s copy" "-copyts -c:s copy"
+
+# This test uses the setts bsf to derive the duration of every packet
+# except the last from the next packet's pts.
+FATE_MATROSKA-$(call REMUX, MATROSKA, SUP_DEMUXER PGS_FRAME_MERGE_BSF SETTS_BSF) += fate-matroska-pgs-remux-durations
+fate-matroska-pgs-remux-durations: CMD = transcode sup $(TARGET_SAMPLES)/sub/pgs_sub.sup matroska "-copyts -c:s copy -bsf pgs_frame_merge,setts=duration=if(gt(DURATION\,0)\,DURATION\,if(eq(PTS\,NOPTS)\,0\,if(eq(NEXT_PTS\,NOPTS)\,0\,NEXT_PTS-PTS))):pts=PTS" "-copyts -c:s copy"
+
FATE_MATROSKA_FFPROBE-$(call ALLYES, MATROSKA_DEMUXER) += fate-matroska-spherical-mono
fate-matroska-spherical-mono: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -show_entries stream_side_data_list -select_streams v -v 0 $(TARGET_SAMPLES)/mkv/spherical.mkv
diff --git a/tests/ref/fate/matroska-pgs-remux b/tests/ref/fate/matroska-pgs-remux
new file mode 100644
index 0000000000..4fab0ffdd8
--- /dev/null
+++ b/tests/ref/fate/matroska-pgs-remux
@@ -0,0 +1,7 @@
+9aa538611b5f3bd0455b2afd3dafe08d *tests/data/fate/matroska-pgs-remux.matroska
+49751 tests/data/fate/matroska-pgs-remux.matroska
+#tb 0: 1/1000
+#media_type 0: subtitle
+#codec_id 0: hdmv_pgs_subtitle
+0, 67, 67, 0, 26127, 0x02389441
+0, 401, 401, 0, 23155, 0x1de74712
diff --git a/tests/ref/fate/matroska-pgs-remux-durations b/tests/ref/fate/matroska-pgs-remux-durations
new file mode 100644
index 0000000000..ba649d0ddf
--- /dev/null
+++ b/tests/ref/fate/matroska-pgs-remux-durations
@@ -0,0 +1,7 @@
+20fa9d515b08718c42d3e3a8a4a8bf5d *tests/data/fate/matroska-pgs-remux-durations.matroska
+49763 tests/data/fate/matroska-pgs-remux-durations.matroska
+#tb 0: 1/1000
+#media_type 0: subtitle
+#codec_id 0: hdmv_pgs_subtitle
+0, 67, 67, 334, 26127, 0x02389441
+0, 401, 401, 0, 23155, 0x1de74712
--
2.32.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".
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments
2022-05-05 10:20 [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments Andreas Rheinhardt
2022-05-05 10:21 ` [FFmpeg-devel] [PATCH 2/3] lavf/matroskaenc: enable PGS merge auto bsf Andreas Rheinhardt
2022-05-05 10:21 ` [FFmpeg-devel] [PATCH 3/3] fate/matroska: Add tests for muxing PGS into Matroska Andreas Rheinhardt
@ 2022-05-09 19:29 ` Andreas Rheinhardt
2 siblings, 0 replies; 4+ messages in thread
From: Andreas Rheinhardt @ 2022-05-09 19:29 UTC (permalink / raw)
To: ffmpeg-devel
Andreas Rheinhardt:
> From: John Stebbins <jstebbins@jetheaddev.com>
>
> Required to remux m2ts to mkv
> Minor changes and porting to FFBitStreamFilter done by the committer.
>
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
> Changelog | 1 +
> doc/bitstream_filters.texi | 8 ++
> libavcodec/Makefile | 1 +
> libavcodec/bitstream_filters.c | 1 +
> libavcodec/pgs_frame_merge_bsf.c | 191 +++++++++++++++++++++++++++++++
> libavcodec/version.h | 2 +-
> 6 files changed, 203 insertions(+), 1 deletion(-)
> create mode 100644 libavcodec/pgs_frame_merge_bsf.c
>
> diff --git a/Changelog b/Changelog
> index 4d467eb741..1bae64075e 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -14,6 +14,7 @@ version 5.1:
> - colormap video filter
> - colorchart video source filter
> - blurdetect filter
> +- PGS subtitle frame merge bitstream filter
>
>
> version 5.0:
> diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
> index fc2c71fc12..50c95f035d 100644
> --- a/doc/bitstream_filters.texi
> +++ b/doc/bitstream_filters.texi
> @@ -695,6 +695,14 @@ for NTSC frame rate using the @option{frame_rate} option.
> ffmpeg -f lavfi -i sine=r=48000:d=1 -c pcm_s16le -bsf pcm_rechunk=r=30000/1001 -f framecrc -
> @end example
>
> +@section pgs_frame_merge
> +
> +Merge a sequence of PGS Subtitle segments ending with an "end of display set"
> +segment into a single packet.
> +
> +This is required by some containers that support PGS subtitles
> +(muxer @code{matroska}).
> +
> @section prores_metadata
>
> Modify color property metadata embedded in prores stream.
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index cfaa6f196a..3c0ff2dc58 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -1195,6 +1195,7 @@ OBJS-$(CONFIG_NOISE_BSF) += noise_bsf.o
> OBJS-$(CONFIG_NULL_BSF) += null_bsf.o
> OBJS-$(CONFIG_OPUS_METADATA_BSF) += opus_metadata_bsf.o
> OBJS-$(CONFIG_PCM_RECHUNK_BSF) += pcm_rechunk_bsf.o
> +OBJS-$(CONFIG_PGS_FRAME_MERGE_BSF) += pgs_frame_merge_bsf.o
> OBJS-$(CONFIG_PRORES_METADATA_BSF) += prores_metadata_bsf.o
> OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o av1_parse.o
> OBJS-$(CONFIG_SETTS_BSF) += setts_bsf.o
> diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
> index f117bc0e17..444423ae93 100644
> --- a/libavcodec/bitstream_filters.c
> +++ b/libavcodec/bitstream_filters.c
> @@ -52,6 +52,7 @@ extern const FFBitStreamFilter ff_noise_bsf;
> extern const FFBitStreamFilter ff_null_bsf;
> extern const FFBitStreamFilter ff_opus_metadata_bsf;
> extern const FFBitStreamFilter ff_pcm_rechunk_bsf;
> +extern const FFBitStreamFilter ff_pgs_frame_merge_bsf;
> extern const FFBitStreamFilter ff_prores_metadata_bsf;
> extern const FFBitStreamFilter ff_remove_extradata_bsf;
> extern const FFBitStreamFilter ff_setts_bsf;
> diff --git a/libavcodec/pgs_frame_merge_bsf.c b/libavcodec/pgs_frame_merge_bsf.c
> new file mode 100644
> index 0000000000..bcd8945335
> --- /dev/null
> +++ b/libavcodec/pgs_frame_merge_bsf.c
> @@ -0,0 +1,191 @@
> +/*
> + * Copyright (c) 2020 John Stebbins <jstebbins.hb@gmail.com>
> + *
> + * 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 merges PGS subtitle packets containing incomplete
> + * set of segments into a single packet
> + *
> + * Packets already containing a complete set of segments will be passed through
> + * unchanged.
> + */
> +
> +#include "libavutil/attributes.h"
> +#include "libavutil/intreadwrite.h"
> +#include "libavutil/log.h"
> +#include "bsf.h"
> +#include "bsf_internal.h"
> +
> +enum PGSSegmentType {
> + PALETTE_SEGMENT = 0x14,
> + OBJECT_SEGMENT = 0x15,
> + PRESENTATION_SEGMENT = 0x16,
> + WINDOW_SEGMENT = 0x17,
> + END_DISPLAY_SET_SEGMENT = 0x80,
> +};
> +
> +typedef struct PGSMergeContext {
> + AVPacket *buffer_pkt, *in;
> + int presentation_found;
> + int pkt_flags;
> +} PGSMergeContext;
> +
> +static av_cold void frame_merge_flush(AVBSFContext *bsf)
> +{
> + PGSMergeContext *ctx = bsf->priv_data;
> +
> + ctx->presentation_found = ctx->pkt_flags = 0;
> + av_packet_unref(ctx->in);
> + av_packet_unref(ctx->buffer_pkt);
> +}
> +
> +static int frame_merge_output(PGSMergeContext *ctx, AVPacket *dst, AVPacket *src)
> +{
> + if (!ctx->presentation_found)
> + ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
> + ctx->presentation_found = 0;
> + src->flags |= ctx->pkt_flags;
> + ctx->pkt_flags = 0;
> + av_packet_move_ref(dst, src);
> + return 0;
> +}
> +
> +static int frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
> +{
> + PGSMergeContext *ctx = bsf->priv_data;
> + AVPacket *in = ctx->in, *pkt = ctx->buffer_pkt;
> + int ret, size, pos, display = 0, presentation = 0;
> + unsigned int i;
> +
> + if (!in->data) {
> + ret = ff_bsf_get_packet_ref(bsf, in);
> + if (ret == AVERROR_EOF && pkt->data) {
> + // Output remaining data
> + ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
> + return frame_merge_output(ctx, out, pkt);
> + }
> + if (ret < 0)
> + return ret;
> + }
> + if (!in->size) {
> + av_packet_unref(in);
> + return AVERROR(EAGAIN);
> + }
> + in->flags &= ~AV_PKT_FLAG_KEY; // Will be detected in the stream
> +
> + // Validate packet data and find display_end segment
> + size = in->size;
> + i = 0;
> + while (i + 3 <= in->size) {
> + uint8_t segment_type = in->data[i];
> + int segment_len = AV_RB16(in->data + i + 1) + 3;
> +
> + if (i + segment_len > in->size)
> + break; // Invalid, segments can't span packets
> + if (segment_type == PRESENTATION_SEGMENT && ctx->presentation_found)
> + break; // Invalid, there can be only one
> + if (segment_type == PRESENTATION_SEGMENT) {
> + uint8_t state;
> + if (segment_len < 11)
> + break; // Invalid presentation segment length
> + ctx->presentation_found = presentation = 1;
> + state = in->data[i + 10] & 0xc0;
> + if (state)
> + ctx->pkt_flags |= AV_PKT_FLAG_KEY;
> + else
> + ctx->pkt_flags &= ~AV_PKT_FLAG_KEY;
> + }
> + i += segment_len;
> + if (segment_type == END_DISPLAY_SET_SEGMENT) {
> + size = i;
> + display = 1;
> + break;
> + }
> + }
> + if (display && pkt->size == 0 && size == in->size) // passthrough
> + return frame_merge_output(ctx, out, in);
> + if (!display && i != in->size) {
> + av_log(bsf, AV_LOG_WARNING, "Failed to parse PGS segments.\n");
> + // force output what we have
> + size = in->size;
> + display = 1;
> + ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
> + }
> +
> + if (presentation) {
> + ret = av_packet_copy_props(pkt, in);
> + if (ret < 0)
> + goto fail;
> + }
> + pos = pkt->size;
> + ret = av_grow_packet(pkt, size);
> + if (ret < 0)
> + goto fail;
> + memcpy(pkt->data + pos, in->data, size);
> +
> + if (size == in->size)
> + av_packet_unref(in);
> + else {
> + in->data += size;
> + in->size -= size;
> + }
> +
> + if (display)
> + return frame_merge_output(ctx, out, pkt);
> + return AVERROR(EAGAIN);
> +
> +fail:
> + frame_merge_flush(bsf);
> + return ret;
> +}
> +
> +static av_cold int frame_merge_init(AVBSFContext *bsf)
> +{
> + PGSMergeContext *ctx = bsf->priv_data;
> +
> + ctx->in = av_packet_alloc();
> + ctx->buffer_pkt = av_packet_alloc();
> + if (!ctx->in || !ctx->buffer_pkt)
> + return AVERROR(ENOMEM);
> +
> + return 0;
> +}
> +
> +static av_cold void frame_merge_close(AVBSFContext *bsf)
> +{
> + PGSMergeContext *ctx = bsf->priv_data;
> +
> + av_packet_free(&ctx->in);
> + av_packet_free(&ctx->buffer_pkt);
> +}
> +
> +static const enum AVCodecID frame_merge_codec_ids[] = {
> + AV_CODEC_ID_HDMV_PGS_SUBTITLE, AV_CODEC_ID_NONE,
> +};
> +
> +const FFBitStreamFilter ff_pgs_frame_merge_bsf = {
> + .p.name = "pgs_frame_merge",
> + .p.codec_ids = frame_merge_codec_ids,
> + .priv_data_size = sizeof(PGSMergeContext),
> + .init = frame_merge_init,
> + .flush = frame_merge_flush,
> + .close = frame_merge_close,
> + .filter = frame_merge_filter,
> +};
> diff --git a/libavcodec/version.h b/libavcodec/version.h
> index 735c8b813c..87b7284a95 100644
> --- a/libavcodec/version.h
> +++ b/libavcodec/version.h
> @@ -29,7 +29,7 @@
>
> #include "version_major.h"
>
> -#define LIBAVCODEC_VERSION_MINOR 27
> +#define LIBAVCODEC_VERSION_MINOR 28
> #define LIBAVCODEC_VERSION_MICRO 100
>
> #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
Will apply this patchset tomorrow unless there are objections.
- 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".
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-05-09 19:29 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-05 10:20 [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments Andreas Rheinhardt
2022-05-05 10:21 ` [FFmpeg-devel] [PATCH 2/3] lavf/matroskaenc: enable PGS merge auto bsf Andreas Rheinhardt
2022-05-05 10:21 ` [FFmpeg-devel] [PATCH 3/3] fate/matroska: Add tests for muxing PGS into Matroska Andreas Rheinhardt
2022-05-09 19:29 ` [FFmpeg-devel] [PATCH 1/3] lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments Andreas Rheinhardt
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git