Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
36 | enum 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 | |||
44 | typedef struct PGSMergeContext { | ||
45 | AVPacket *buffer_pkt, *in; | ||
46 | int presentation_found; | ||
47 | int pkt_flags; | ||
48 | } PGSMergeContext; | ||
49 | |||
50 | ✗ | static 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 | |||
59 | 6 | static int frame_merge_output(PGSMergeContext *ctx, AVPacket *dst, AVPacket *src) | |
60 | { | ||
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!ctx->presentation_found) |
62 | ✗ | ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT; | |
63 | 6 | ctx->presentation_found = 0; | |
64 | 6 | src->flags |= ctx->pkt_flags; | |
65 | 6 | ctx->pkt_flags = 0; | |
66 | 6 | av_packet_move_ref(dst, src); | |
67 | 6 | return 0; | |
68 | } | ||
69 | |||
70 | 31 | static int frame_merge_filter(AVBSFContext *bsf, AVPacket *out) | |
71 | { | ||
72 | 31 | PGSMergeContext *ctx = bsf->priv_data; | |
73 | 31 | AVPacket *in = ctx->in, *pkt = ctx->buffer_pkt; | |
74 | 31 | int ret, size, pos, display = 0, presentation = 0; | |
75 | unsigned int i; | ||
76 | |||
77 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (!in->data) { |
78 | 31 | ret = ff_bsf_get_packet_ref(bsf, in); | |
79 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
|
31 | if (ret == AVERROR_EOF && pkt->data) { |
80 | // Output remaining data | ||
81 | 3 | ctx->pkt_flags |= AV_PKT_FLAG_CORRUPT; | |
82 | 3 | return frame_merge_output(ctx, out, pkt); | |
83 | } | ||
84 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 22 times.
|
28 | if (ret < 0) |
85 | 6 | return ret; | |
86 | } | ||
87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (!in->size) { |
88 | ✗ | av_packet_unref(in); | |
89 | ✗ | return AVERROR(EAGAIN); | |
90 | } | ||
91 | 22 | in->flags &= ~AV_PKT_FLAG_KEY; // Will be detected in the stream | |
92 | |||
93 | // Validate packet data and find display_end segment | ||
94 | 22 | size = in->size; | |
95 | 22 | i = 0; | |
96 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 19 times.
|
49 | while (i + 3 <= in->size) { |
97 | 30 | uint8_t segment_type = in->data[i]; | |
98 | 30 | int segment_len = AV_RB16(in->data + i + 1) + 3; | |
99 | |||
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (i + segment_len > in->size) |
101 | ✗ | break; // Invalid, segments can't span packets | |
102 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
30 | if (segment_type == PRESENTATION_SEGMENT && ctx->presentation_found) |
103 | ✗ | break; // Invalid, there can be only one | |
104 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 24 times.
|
30 | if (segment_type == PRESENTATION_SEGMENT) { |
105 | uint8_t state; | ||
106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (segment_len < 11) |
107 | ✗ | break; // Invalid presentation segment length | |
108 | 6 | ctx->presentation_found = presentation = 1; | |
109 | 6 | state = in->data[i + 10] & 0xc0; | |
110 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (state) |
111 | 6 | ctx->pkt_flags |= AV_PKT_FLAG_KEY; | |
112 | else | ||
113 | ✗ | ctx->pkt_flags &= ~AV_PKT_FLAG_KEY; | |
114 | } | ||
115 | 30 | i += segment_len; | |
116 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27 times.
|
30 | if (segment_type == END_DISPLAY_SET_SEGMENT) { |
117 | 3 | size = i; | |
118 | 3 | display = 1; | |
119 | 3 | break; | |
120 | } | ||
121 | } | ||
122 |
5/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
22 | if (display && pkt->size == 0 && size == in->size) // passthrough |
123 | 1 | return frame_merge_output(ctx, out, in); | |
124 |
3/4✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
|
21 | 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 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 16 times.
|
21 | if (presentation) { |
133 | 5 | ret = av_packet_copy_props(pkt, in); | |
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (ret < 0) |
135 | ✗ | goto fail; | |
136 | } | ||
137 | 21 | pos = pkt->size; | |
138 | 21 | ret = av_grow_packet(pkt, size); | |
139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
|
21 | if (ret < 0) |
140 | ✗ | goto fail; | |
141 | 21 | memcpy(pkt->data + pos, in->data, size); | |
142 | |||
143 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | if (size == in->size) |
144 | 21 | av_packet_unref(in); | |
145 | else { | ||
146 | ✗ | in->data += size; | |
147 | ✗ | in->size -= size; | |
148 | } | ||
149 | |||
150 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
|
21 | if (display) |
151 | 2 | return frame_merge_output(ctx, out, pkt); | |
152 | 19 | return AVERROR(EAGAIN); | |
153 | |||
154 | ✗ | fail: | |
155 | ✗ | frame_merge_flush(bsf); | |
156 | ✗ | return ret; | |
157 | } | ||
158 | |||
159 | 3 | static av_cold int frame_merge_init(AVBSFContext *bsf) | |
160 | { | ||
161 | 3 | PGSMergeContext *ctx = bsf->priv_data; | |
162 | |||
163 | 3 | ctx->in = av_packet_alloc(); | |
164 | 3 | ctx->buffer_pkt = av_packet_alloc(); | |
165 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (!ctx->in || !ctx->buffer_pkt) |
166 | ✗ | return AVERROR(ENOMEM); | |
167 | |||
168 | 3 | return 0; | |
169 | } | ||
170 | |||
171 | 3 | static av_cold void frame_merge_close(AVBSFContext *bsf) | |
172 | { | ||
173 | 3 | PGSMergeContext *ctx = bsf->priv_data; | |
174 | |||
175 | 3 | av_packet_free(&ctx->in); | |
176 | 3 | av_packet_free(&ctx->buffer_pkt); | |
177 | 3 | } | |
178 | |||
179 | static const enum AVCodecID frame_merge_codec_ids[] = { | ||
180 | AV_CODEC_ID_HDMV_PGS_SUBTITLE, AV_CODEC_ID_NONE, | ||
181 | }; | ||
182 | |||
183 | const 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 | }; | ||
192 |