History

lavc/pgs_frame_merge_bsf: add bsf to merge PGS segments

Required to remux m2ts to mkv Minor changes and porting to FFBitStreamFilter done by the committer.

John Stebbins committed 6 years ago

Commit 326f9fd1 parent 38238b66 files · +203 −1Browse files

Unified · Split

6 files
  1. Changelog +1 −0
  2. doc/bitstream_filters.texi +8 −0
  3. libavcodec/Makefile +1 −0
  4. libavcodec/bitstream_filters.c +1 −0
  5. libavcodec/pgs_frame_merge_bsf.c +191 −0
  6. libavcodec/version.h +1 −1
Changelog+1−0
@@ -15,6 +15,7 @@ version 5.1: · 14 unmodified lines
15- colorchart video source filter15- colorchart video source filter
16- blurdetect filter16- blurdetect filter
17- multiply video filter17- multiply video filter
18- PGS subtitle frame merge bitstream filter
18 19
19 20
20version 5.0:21version 5.0:
doc/bitstream_filters.texi+8−0
@@ -695,6 +695,14 @@ for NTSC frame rate using the @option{frame_rate} option. · 694 unmodified lines
695ffmpeg -f lavfi -i sine=r=48000:d=1 -c pcm_s16le -bsf pcm_rechunk=r=30000/1001 -f framecrc -695ffmpeg -f lavfi -i sine=r=48000:d=1 -c pcm_s16le -bsf pcm_rechunk=r=30000/1001 -f framecrc -
696@end example696@end example
697 697
698@section pgs_frame_merge
699
700Merge a sequence of PGS Subtitle segments ending with an "end of display set"
701segment into a single packet.
702
703This is required by some containers that support PGS subtitles
704(muxer @code{matroska}).
705
698@section prores_metadata706@section prores_metadata
699 707
700Modify color property metadata embedded in prores stream.708Modify color property metadata embedded in prores stream.
libavcodec/Makefile+1−0
@@ -1196,6 +1196,7 @@ OBJS-$(CONFIG_NOISE_BSF) += noise_bsf.o · 1195 unmodified lines
1196OBJS-$(CONFIG_NULL_BSF) += null_bsf.o1196OBJS-$(CONFIG_NULL_BSF) += null_bsf.o
1197OBJS-$(CONFIG_OPUS_METADATA_BSF) += opus_metadata_bsf.o1197OBJS-$(CONFIG_OPUS_METADATA_BSF) += opus_metadata_bsf.o
1198OBJS-$(CONFIG_PCM_RECHUNK_BSF) += pcm_rechunk_bsf.o1198OBJS-$(CONFIG_PCM_RECHUNK_BSF) += pcm_rechunk_bsf.o
1199OBJS-$(CONFIG_PGS_FRAME_MERGE_BSF) += pgs_frame_merge_bsf.o
1199OBJS-$(CONFIG_PRORES_METADATA_BSF) += prores_metadata_bsf.o1200OBJS-$(CONFIG_PRORES_METADATA_BSF) += prores_metadata_bsf.o
1200OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o av1_parse.o1201OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o av1_parse.o
1201OBJS-$(CONFIG_SETTS_BSF) += setts_bsf.o1202OBJS-$(CONFIG_SETTS_BSF) += setts_bsf.o
libavcodec/bitstream_filters.c+1−0
@@ -52,6 +52,7 @@ extern const FFBitStreamFilter ff_noise_bsf; · 51 unmodified lines
52extern const FFBitStreamFilter ff_null_bsf;52extern const FFBitStreamFilter ff_null_bsf;
53extern const FFBitStreamFilter ff_opus_metadata_bsf;53extern const FFBitStreamFilter ff_opus_metadata_bsf;
54extern const FFBitStreamFilter ff_pcm_rechunk_bsf;54extern const FFBitStreamFilter ff_pcm_rechunk_bsf;
55extern const FFBitStreamFilter ff_pgs_frame_merge_bsf;
55extern const FFBitStreamFilter ff_prores_metadata_bsf;56extern const FFBitStreamFilter ff_prores_metadata_bsf;
56extern const FFBitStreamFilter ff_remove_extradata_bsf;57extern const FFBitStreamFilter ff_remove_extradata_bsf;
57extern const FFBitStreamFilter ff_setts_bsf;58extern const FFBitStreamFilter ff_setts_bsf;
libavcodec/pgs_frame_merge_bsf.c+191−0
@@ -0,0 +1,191 @@
1/*
2 * Copyright (c) 2020 John Stebbins <jstebbins.hb@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * This bitstream filter merges PGS subtitle packets containing incomplete
24 * set of segments into a single packet
25 *
26 * Packets already containing a complete set of segments will be passed through
27 * unchanged.
28 */
29
30#include "libavutil/attributes.h"
31#include "libavutil/intreadwrite.h"
32#include "libavutil/log.h"
33#include "bsf.h"
34#include "bsf_internal.h"
35
36enum PGSSegmentType {
37 PALETTE_SEGMENT = 0x14,
38 OBJECT_SEGMENT = 0x15,
39 PRESENTATION_SEGMENT = 0x16,
40 WINDOW_SEGMENT = 0x17,
41 END_DISPLAY_SET_SEGMENT = 0x80,
42};
43
44typedef struct PGSMergeContext {
45 AVPacket *buffer_pkt, *in;
46 int presentation_found;
47 int pkt_flags;
48} PGSMergeContext;
49
50static av_cold void frame_merge_flush(AVBSFContext *bsf)
51{
52 PGSMergeContext *ctx = bsf->priv_data;
53
54 ctx->presentation_found = ctx->pkt_flags = 0;
55 av_packet_unref(ctx->in);
56 av_packet_unref(ctx->buffer_pkt);
57}
58
59static int frame_merge_output(PGSMergeContext *ctx, AVPacket *dst, AVPacket *src)
60{
61 if (!ctx->presentation_found)
62 ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
63 ctx->presentation_found = 0;
64 src->flags |= ctx->pkt_flags;
65 ctx->pkt_flags = 0;
66 av_packet_move_ref(dst, src);
67 return 0;
68}
69
70static int frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
71{
72 PGSMergeContext *ctx = bsf->priv_data;
73 AVPacket *in = ctx->in, *pkt = ctx->buffer_pkt;
74 int ret, size, pos, display = 0, presentation = 0;
75 unsigned int i;
76
77 if (!in->data) {
78 ret = ff_bsf_get_packet_ref(bsf, in);
79 if (ret == AVERROR_EOF && pkt->data) {
80 // Output remaining data
81 ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
82 return frame_merge_output(ctx, out, pkt);
83 }
84 if (ret < 0)
85 return ret;
86 }
87 if (!in->size) {
88 av_packet_unref(in);
89 return AVERROR(EAGAIN);
90 }
91 in->flags &= ~AV_PKT_FLAG_KEY; // Will be detected in the stream
92
93 // Validate packet data and find display_end segment
94 size = in->size;
95 i = 0;
96 while (i + 3 <= in->size) {
97 uint8_t segment_type = in->data[i];
98 int segment_len = AV_RB16(in->data + i + 1) + 3;
99
100 if (i + segment_len > in->size)
101 break; // Invalid, segments can't span packets
102 if (segment_type == PRESENTATION_SEGMENT && ctx->presentation_found)
103 break; // Invalid, there can be only one
104 if (segment_type == PRESENTATION_SEGMENT) {
105 uint8_t state;
106 if (segment_len < 11)
107 break; // Invalid presentation segment length
108 ctx->presentation_found = presentation = 1;
109 state = in->data[i + 10] & 0xc0;
110 if (state)
111 ctx->pkt_flags |= AV_PKT_FLAG_KEY;
112 else
113 ctx->pkt_flags &= ~AV_PKT_FLAG_KEY;
114 }
115 i += segment_len;
116 if (segment_type == END_DISPLAY_SET_SEGMENT) {
117 size = i;
118 display = 1;
119 break;
120 }
121 }
122 if (display && pkt->size == 0 && size == in->size) // passthrough
123 return frame_merge_output(ctx, out, in);
124 if (!display && i != in->size) {
125 av_log(bsf, AV_LOG_WARNING, "Failed to parse PGS segments.\n");
126 // force output what we have
127 size = in->size;
128 display = 1;
129 ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT;
130 }
131
132 if (presentation) {
133 ret = av_packet_copy_props(pkt, in);
134 if (ret < 0)
135 goto fail;
136 }
137 pos = pkt->size;
138 ret = av_grow_packet(pkt, size);
139 if (ret < 0)
140 goto fail;
141 memcpy(pkt->data + pos, in->data, size);
142
143 if (size == in->size)
144 av_packet_unref(in);
145 else {
146 in->data += size;
147 in->size -= size;
148 }
149
150 if (display)
151 return frame_merge_output(ctx, out, pkt);
152 return AVERROR(EAGAIN);
153
154fail:
155 frame_merge_flush(bsf);
156 return ret;
157}
158
159static av_cold int frame_merge_init(AVBSFContext *bsf)
160{
161 PGSMergeContext *ctx = bsf->priv_data;
162
163 ctx->in = av_packet_alloc();
164 ctx->buffer_pkt = av_packet_alloc();
165 if (!ctx->in || !ctx->buffer_pkt)
166 return AVERROR(ENOMEM);
167
168 return 0;
169}
170
171static av_cold void frame_merge_close(AVBSFContext *bsf)
172{
173 PGSMergeContext *ctx = bsf->priv_data;
174
175 av_packet_free(&ctx->in);
176 av_packet_free(&ctx->buffer_pkt);
177}
178
179static const enum AVCodecID frame_merge_codec_ids[] = {
180 AV_CODEC_ID_HDMV_PGS_SUBTITLE, AV_CODEC_ID_NONE,
181};
182
183const FFBitStreamFilter ff_pgs_frame_merge_bsf = {
184 .p.name = "pgs_frame_merge",
185 .p.codec_ids = frame_merge_codec_ids,
186 .priv_data_size = sizeof(PGSMergeContext),
187 .init = frame_merge_init,
188 .flush = frame_merge_flush,
189 .close = frame_merge_close,
190 .filter = frame_merge_filter,
191};
libavcodec/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
29 29
30#include "version_major.h"30#include "version_major.h"
31 31
32#define LIBAVCODEC_VERSION_MINOR 2732#define LIBAVCODEC_VERSION_MINOR 28
33#define LIBAVCODEC_VERSION_MICRO 10033#define LIBAVCODEC_VERSION_MICRO 100
34 34
35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t