srtdec.c 9.35 KB
Newer Older
Aurelien Jacobs's avatar
Aurelien Jacobs committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * SubRip subtitle decoder
 * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
 *
 * 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
 */

#include "libavutil/avstring.h"
23
#include "libavutil/common.h"
24
#include "libavutil/intreadwrite.h"
25
#include "libavutil/parseutils.h"
Aurelien Jacobs's avatar
Aurelien Jacobs committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "avcodec.h"
#include "ass.h"

static int html_color_parse(AVCodecContext *avctx, const char *str)
{
    uint8_t rgba[4];
    if (av_parse_color(rgba, str, strcspn(str, "\" >"), avctx) < 0)
        return -1;
    return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
}

enum {
    PARAM_UNKNOWN = -1,
    PARAM_SIZE,
    PARAM_COLOR,
    PARAM_FACE,
    PARAM_NUMBER
};

typedef struct {
    char tag[128];
    char param[PARAM_NUMBER][128];
} SrtStack;

50 51 52 53 54 55 56 57
static void rstrip_spaces_buf(AVBPrint *buf)
{
    while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
        buf->str[--buf->len] = 0;
}

static void srt_to_ass(AVCodecContext *avctx, AVBPrint *dst,
                       const char *in, int x1, int y1, int x2, int y2)
Aurelien Jacobs's avatar
Aurelien Jacobs committed
58
{
59
    char *param, buffer[128], tmp[128];
Aurelien Jacobs's avatar
Aurelien Jacobs committed
60 61 62 63 64 65 66 67 68 69
    int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
    SrtStack stack[16];

    stack[0].tag[0] = 0;
    strcpy(stack[0].param[PARAM_SIZE],  "{\\fs}");
    strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
    strcpy(stack[0].param[PARAM_FACE],  "{\\fn}");

    if (x1 >= 0 && y1 >= 0) {
        if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1))
70
            av_bprintf(dst, "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
71
        else
72
            av_bprintf(dst, "{\\an1}{\\pos(%d,%d)}", x1, y1);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
73 74
    }

75
    for (; !end && *in; in++) {
Aurelien Jacobs's avatar
Aurelien Jacobs committed
76 77 78 79 80 81 82 83
        switch (*in) {
        case '\r':
            break;
        case '\n':
            if (line_start) {
                end = 1;
                break;
            }
84 85
            rstrip_spaces_buf(dst);
            av_bprintf(dst, "\\N");
Aurelien Jacobs's avatar
Aurelien Jacobs committed
86 87 88 89
            line_start = 1;
            break;
        case ' ':
            if (!line_start)
90
                av_bprint_chars(dst, *in, 1);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
91 92 93
            break;
        case '{':    /* skip all {\xxx} substrings except for {\an%d}
                        and all microdvd like styles such as {Y:xxx} */
94 95 96 97
            len = 0;
            an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0;
            if ((an != 1 && (len = 0, sscanf(in, "{\\%*[^}]}%n", &len) >= 0 && len > 0)) ||
                (len = 0, sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n", &len) >= 0 && len > 0)) {
Aurelien Jacobs's avatar
Aurelien Jacobs committed
98 99
                in += len - 1;
            } else
100
                av_bprint_chars(dst, *in, 1);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
101 102 103
            break;
        case '<':
            tag_close = in[1] == '/';
104 105
            len = 0;
            if (sscanf(in+tag_close+1, "%127[^>]>%n", buffer, &len) >= 1 && len > 0) {
Aurelien Jacobs's avatar
Aurelien Jacobs committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119
                if ((param = strchr(buffer, ' ')))
                    *param++ = 0;
                if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
                    ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, buffer))) {
                    int i, j, unknown = 0;
                    in += len + tag_close;
                    if (!tag_close)
                        memset(stack+sptr, 0, sizeof(*stack));
                    if (!strcmp(buffer, "font")) {
                        if (tag_close) {
                            for (i=PARAM_NUMBER-1; i>=0; i--)
                                if (stack[sptr-1].param[i][0])
                                    for (j=sptr-2; j>=0; j--)
                                        if (stack[j].param[i][0]) {
120
                                            av_bprintf(dst, "%s", stack[j].param[i]);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
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
                                            break;
                                        }
                        } else {
                            while (param) {
                                if (!strncmp(param, "size=", 5)) {
                                    unsigned font_size;
                                    param += 5 + (param[5] == '"');
                                    if (sscanf(param, "%u", &font_size) == 1) {
                                        snprintf(stack[sptr].param[PARAM_SIZE],
                                             sizeof(stack[0].param[PARAM_SIZE]),
                                             "{\\fs%u}", font_size);
                                    }
                                } else if (!strncmp(param, "color=", 6)) {
                                    param += 6 + (param[6] == '"');
                                    snprintf(stack[sptr].param[PARAM_COLOR],
                                         sizeof(stack[0].param[PARAM_COLOR]),
                                         "{\\c&H%X&}",
                                         html_color_parse(avctx, param));
                                } else if (!strncmp(param, "face=", 5)) {
                                    param += 5 + (param[5] == '"');
                                    len = strcspn(param,
                                                  param[-1] == '"' ? "\"" :" ");
                                    av_strlcpy(tmp, param,
                                               FFMIN(sizeof(tmp), len+1));
                                    param += len;
                                    snprintf(stack[sptr].param[PARAM_FACE],
                                             sizeof(stack[0].param[PARAM_FACE]),
                                             "{\\fn%s}", tmp);
                                }
                                if ((param = strchr(param, ' ')))
                                    param++;
                            }
                            for (i=0; i<PARAM_NUMBER; i++)
154 155
                                if (stack[sptr].param[i][0])
                                    av_bprintf(dst, "%s", stack[sptr].param[i]);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
156 157
                        }
                    } else if (!buffer[1] && strspn(buffer, "bisu") == 1) {
158
                        av_bprintf(dst, "{\\%c%d}", buffer[0], !tag_close);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
159 160 161 162 163 164 165 166
                    } else {
                        unknown = 1;
                        snprintf(tmp, sizeof(tmp), "</%s>", buffer);
                    }
                    if (tag_close) {
                        sptr--;
                    } else if (unknown && !strstr(in, tmp)) {
                        in -= len + tag_close;
167
                        av_bprint_chars(dst, *in, 1);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
168 169 170 171 172 173 174
                    } else
                        av_strlcpy(stack[sptr++].tag, buffer,
                                   sizeof(stack[0].tag));
                    break;
                }
            }
        default:
175
            av_bprint_chars(dst, *in, 1);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
176 177 178 179 180 181
            break;
        }
        if (*in != ' ' && *in != '\r' && *in != '\n')
            line_start = 0;
    }

182 183 184 185
    while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
        dst->len -= 2;
    dst->str[dst->len] = 0;
    rstrip_spaces_buf(dst);
Aurelien Jacobs's avatar
Aurelien Jacobs committed
186 187 188 189 190 191
}

static int srt_decode_frame(AVCodecContext *avctx,
                            void *data, int *got_sub_ptr, AVPacket *avpkt)
{
    AVSubtitle *sub = data;
192
    AVBPrint buffer;
Aurelien Jacobs's avatar
Aurelien Jacobs committed
193
    int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
194
    int size, ret;
195 196 197 198 199 200 201 202
    const uint8_t *p = av_packet_get_side_data(avpkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);

    if (p && size == 16) {
        x1 = AV_RL32(p     );
        y1 = AV_RL32(p +  4);
        x2 = AV_RL32(p +  8);
        y2 = AV_RL32(p + 12);
    }
Aurelien Jacobs's avatar
Aurelien Jacobs committed
203 204 205 206

    if (avpkt->size <= 0)
        return avpkt->size;

207 208
    av_bprint_init(&buffer, 0, AV_BPRINT_SIZE_UNLIMITED);

Clément Bœsch's avatar
Clément Bœsch committed
209
        // TODO: reindent
210 211 212 213 214 215 216
            // Do final divide-by-10 outside rescale to force rounding down.
            ts_start = av_rescale_q(avpkt->pts,
                                    avctx->time_base,
                                    (AVRational){1,100});
            ts_end   = av_rescale_q(avpkt->pts + avpkt->duration,
                                    avctx->time_base,
                                    (AVRational){1,100});
217 218

    srt_to_ass(avctx, &buffer, avpkt->data, x1, y1, x2, y2);
219
    ret = ff_ass_add_rect_bprint(sub, &buffer, ts_start, ts_end-ts_start);
220 221 222
    av_bprint_finalize(&buffer, NULL);
    if (ret < 0)
        return ret;
Aurelien Jacobs's avatar
Aurelien Jacobs committed
223 224 225 226 227

    *got_sub_ptr = sub->num_rects > 0;
    return avpkt->size;
}

228
#if CONFIG_SRT_DECODER
229
/* deprecated decoder */
230
AVCodec ff_srt_decoder = {
Aurelien Jacobs's avatar
Aurelien Jacobs committed
231
    .name         = "srt",
Clément Bœsch's avatar
Clément Bœsch committed
232
    .long_name    = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
Aurelien Jacobs's avatar
Aurelien Jacobs committed
233
    .type         = AVMEDIA_TYPE_SUBTITLE,
Clément Bœsch's avatar
Clément Bœsch committed
234
    .id           = AV_CODEC_ID_SUBRIP,
Aurelien Jacobs's avatar
Aurelien Jacobs committed
235 236 237
    .init         = ff_ass_subtitle_header_default,
    .decode       = srt_decode_frame,
};
238 239 240 241 242 243 244 245 246 247 248 249
#endif

#if CONFIG_SUBRIP_DECODER
AVCodec ff_subrip_decoder = {
    .name         = "subrip",
    .long_name    = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
    .type         = AVMEDIA_TYPE_SUBTITLE,
    .id           = AV_CODEC_ID_SUBRIP,
    .init         = ff_ass_subtitle_header_default,
    .decode       = srt_decode_frame,
};
#endif