aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/pgs_frame_merge_bsf.c
blob: bcd8945335a5f3efcbe8cb4532f0e36781c49802 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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,
};