FFmpeg
Loading...
Searching...
No Matches
pgs_frame_merge.c
Go to the documentation of this file.
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
32#include "libavutil/log.h"
33#include "libavcodec/bsf.h"
35
43
49
51{
53
54 ctx->presentation_found = ctx->pkt_flags = 0;
56 av_packet_unref(ctx->buffer_pkt);
57}
58
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;
67 return 0;
68}
69
71{
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) {
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:
156 return ret;
157}
158
160{
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
172{
174
175 av_packet_free(&ctx->in);
176 av_packet_free(&ctx->buffer_pkt);
177}
178
182
184 .p.name = "pgs_frame_merge",
185 .p.codec_ids = frame_merge_codec_ids,
186 .priv_data_size = sizeof(PGSMergeContext),
191};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
const FFBitStreamFilter ff_pgs_frame_merge_bsf
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition bsf.c:254
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static AVPacket * pkt
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:610
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static struct @346255127015250356166251341105367306144006377143 state
#define fail
Definition test.h:479
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition codec_id.h:572
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition packet.c:121
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:491
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition packet.c:397
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_RB16(p)
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
static enum AVCodecID frame_merge_codec_ids[]
static int frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
static av_cold int frame_merge_init(AVBSFContext *bsf)
static av_cold void frame_merge_close(AVBSFContext *bsf)
PGSSegmentType
@ OBJECT_SEGMENT
@ END_DISPLAY_SET_SEGMENT
@ PALETTE_SEGMENT
@ PRESENTATION_SEGMENT
@ WINDOW_SEGMENT
static int frame_merge_output(PGSMergeContext *ctx, AVPacket *dst, AVPacket *src)
static av_cold void frame_merge_flush(AVBSFContext *bsf)
unsigned int pos
Definition spdifenc.c:431
The bitstream filter state.
Definition bsf.h:68
void * priv_data
Opaque filter-specific private data.
Definition bsf.h:83
This structure stores compressed data.
Definition packet.h:580
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:609
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVPacket * buffer_pkt
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
int size