/* * SubRip subtitle decoder * Copyright (c) 2010 Aurelien Jacobs * * 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" #include "libavutil/common.h" #include "libavutil/intreadwrite.h" #include "libavutil/parseutils.h" #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; static const char *srt_to_ass(AVCodecContext *avctx, char *out, char *out_end, const char *in, int x1, int y1, int x2, int y2) { char *param, buffer[128], tmp[128]; 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)) snprintf(out, out_end-out, "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2); else snprintf(out, out_end-out, "{\\an1}{\\pos(%d,%d)}", x1, y1); out += strlen(out); } for (; out < out_end && !end && *in; in++) { switch (*in) { case '\r': break; case '\n': if (line_start) { end = 1; break; } while (out[-1] == ' ') out--; snprintf(out, out_end-out, "\\N"); if(out= 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)) { in += len - 1; } else *out++ = *in; break; case '<': tag_close = in[1] == '/'; len = 0; if (sscanf(in+tag_close+1, "%127[^>]>%n", buffer, &len) >= 1 && len > 0) { 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]) { snprintf(out, out_end-out, "%s", stack[j].param[i]); if(out", buffer); } if (tag_close) { sptr--; } else if (unknown && !strstr(in, tmp)) { in -= len + tag_close; *out++ = *in; } else av_strlcpy(stack[sptr++].tag, buffer, sizeof(stack[0].tag)); break; } } default: *out++ = *in; break; } if (*in != ' ' && *in != '\r' && *in != '\n') line_start = 0; } out = FFMIN(out, out_end-3); while (!strncmp(out-2, "\\N", 2)) out -= 2; while (out[-1] == ' ') out--; snprintf(out, out_end-out, "\r\n"); return in; } static int srt_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr, AVPacket *avpkt) { AVSubtitle *sub = data; int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1; char buffer[2048]; const char *ptr = avpkt->data; const char *end = avpkt->data + avpkt->size; int size, ret; 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); } if (avpkt->size <= 0) return avpkt->size; while (ptr < end && *ptr) { // TODO: reindent // 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}); ptr = srt_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr, x1, y1, x2, y2); ret = ff_ass_add_rect(sub, buffer, ts_start, ts_end-ts_start, 0); if (ret < 0) return ret; } *got_sub_ptr = sub->num_rects > 0; return avpkt->size; } #if CONFIG_SRT_DECODER /* deprecated decoder */ AVCodec ff_srt_decoder = { .name = "srt", .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 #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