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};
192