Libav 0.7.1
|
00001 /* 00002 * SubRip subtitle decoder 00003 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "libavutil/avstring.h" 00023 #include "libavutil/parseutils.h" 00024 #include "avcodec.h" 00025 #include "ass.h" 00026 00027 static int html_color_parse(AVCodecContext *avctx, const char *str) 00028 { 00029 uint8_t rgba[4]; 00030 if (av_parse_color(rgba, str, strcspn(str, "\" >"), avctx) < 0) 00031 return -1; 00032 return rgba[0] | rgba[1] << 8 | rgba[2] << 16; 00033 } 00034 00035 enum { 00036 PARAM_UNKNOWN = -1, 00037 PARAM_SIZE, 00038 PARAM_COLOR, 00039 PARAM_FACE, 00040 PARAM_NUMBER 00041 }; 00042 00043 typedef struct { 00044 char tag[128]; 00045 char param[PARAM_NUMBER][128]; 00046 } SrtStack; 00047 00048 static const char *srt_to_ass(AVCodecContext *avctx, char *out, char *out_end, 00049 const char *in, int x1, int y1, int x2, int y2) 00050 { 00051 char c, *param, buffer[128], tmp[128]; 00052 int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0; 00053 SrtStack stack[16]; 00054 00055 stack[0].tag[0] = 0; 00056 strcpy(stack[0].param[PARAM_SIZE], "{\\fs}"); 00057 strcpy(stack[0].param[PARAM_COLOR], "{\\c}"); 00058 strcpy(stack[0].param[PARAM_FACE], "{\\fn}"); 00059 00060 if (x1 >= 0 && y1 >= 0) { 00061 if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1)) 00062 out += snprintf(out, out_end-out, 00063 "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2); 00064 else 00065 out += snprintf(out, out_end-out, "{\\an1}{\\pos(%d,%d)}", x1, y1); 00066 } 00067 00068 for (; out < out_end && !end && *in; in++) { 00069 switch (*in) { 00070 case '\r': 00071 break; 00072 case '\n': 00073 if (line_start) { 00074 end = 1; 00075 break; 00076 } 00077 while (out[-1] == ' ') 00078 out--; 00079 out += snprintf(out, out_end-out, "\\N"); 00080 line_start = 1; 00081 break; 00082 case ' ': 00083 if (!line_start) 00084 *out++ = *in; 00085 break; 00086 case '{': /* skip all {\xxx} substrings except for {\an%d} 00087 and all microdvd like styles such as {Y:xxx} */ 00088 an += sscanf(in, "{\\an%*1u}%c", &c) == 1; 00089 if ((an != 1 && sscanf(in, "{\\%*[^}]}%n%c", &len, &c) > 0) || 00090 sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n%c", &len, &c) > 0) { 00091 in += len - 1; 00092 } else 00093 *out++ = *in; 00094 break; 00095 case '<': 00096 tag_close = in[1] == '/'; 00097 if (sscanf(in+tag_close+1, "%127[^>]>%n%c", buffer, &len,&c) >= 2) { 00098 if ((param = strchr(buffer, ' '))) 00099 *param++ = 0; 00100 if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) || 00101 ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, buffer))) { 00102 int i, j, unknown = 0; 00103 in += len + tag_close; 00104 if (!tag_close) 00105 memset(stack+sptr, 0, sizeof(*stack)); 00106 if (!strcmp(buffer, "font")) { 00107 if (tag_close) { 00108 for (i=PARAM_NUMBER-1; i>=0; i--) 00109 if (stack[sptr-1].param[i][0]) 00110 for (j=sptr-2; j>=0; j--) 00111 if (stack[j].param[i][0]) { 00112 out += snprintf(out, out_end-out, 00113 "%s", stack[j].param[i]); 00114 break; 00115 } 00116 } else { 00117 while (param) { 00118 if (!strncmp(param, "size=", 5)) { 00119 unsigned font_size; 00120 param += 5 + (param[5] == '"'); 00121 if (sscanf(param, "%u", &font_size) == 1) { 00122 snprintf(stack[sptr].param[PARAM_SIZE], 00123 sizeof(stack[0].param[PARAM_SIZE]), 00124 "{\\fs%u}", font_size); 00125 } 00126 } else if (!strncmp(param, "color=", 6)) { 00127 param += 6 + (param[6] == '"'); 00128 snprintf(stack[sptr].param[PARAM_COLOR], 00129 sizeof(stack[0].param[PARAM_COLOR]), 00130 "{\\c&H%X&}", 00131 html_color_parse(avctx, param)); 00132 } else if (!strncmp(param, "face=", 5)) { 00133 param += 5 + (param[5] == '"'); 00134 len = strcspn(param, 00135 param[-1] == '"' ? "\"" :" "); 00136 av_strlcpy(tmp, param, 00137 FFMIN(sizeof(tmp), len+1)); 00138 param += len; 00139 snprintf(stack[sptr].param[PARAM_FACE], 00140 sizeof(stack[0].param[PARAM_FACE]), 00141 "{\\fn%s}", tmp); 00142 } 00143 if ((param = strchr(param, ' '))) 00144 param++; 00145 } 00146 for (i=0; i<PARAM_NUMBER; i++) 00147 if (stack[sptr].param[i][0]) 00148 out += snprintf(out, out_end-out, 00149 "%s", stack[sptr].param[i]); 00150 } 00151 } else if (!buffer[1] && strspn(buffer, "bisu") == 1) { 00152 out += snprintf(out, out_end-out, 00153 "{\\%c%d}", buffer[0], !tag_close); 00154 } else { 00155 unknown = 1; 00156 snprintf(tmp, sizeof(tmp), "</%s>", buffer); 00157 } 00158 if (tag_close) { 00159 sptr--; 00160 } else if (unknown && !strstr(in, tmp)) { 00161 in -= len + tag_close; 00162 *out++ = *in; 00163 } else 00164 av_strlcpy(stack[sptr++].tag, buffer, 00165 sizeof(stack[0].tag)); 00166 break; 00167 } 00168 } 00169 default: 00170 *out++ = *in; 00171 break; 00172 } 00173 if (*in != ' ' && *in != '\r' && *in != '\n') 00174 line_start = 0; 00175 } 00176 00177 out = FFMIN(out, out_end-3); 00178 while (!strncmp(out-2, "\\N", 2)) 00179 out -= 2; 00180 while (out[-1] == ' ') 00181 out--; 00182 out += snprintf(out, out_end-out, "\r\n"); 00183 return in; 00184 } 00185 00186 static const char *read_ts(const char *buf, int *ts_start, int *ts_end, 00187 int *x1, int *y1, int *x2, int *y2) 00188 { 00189 int i, hs, ms, ss, he, me, se; 00190 00191 for (i=0; i<2; i++) { 00192 /* try to read timestamps in either the first or second line */ 00193 int c = sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d" 00194 "%*[ ]X1:%u X2:%u Y1:%u Y2:%u", 00195 &hs, &ms, &ss, ts_start, &he, &me, &se, ts_end, 00196 x1, x2, y1, y2); 00197 buf += strcspn(buf, "\n") + 1; 00198 if (c >= 8) { 00199 *ts_start = 100*(ss + 60*(ms + 60*hs)) + *ts_start/10; 00200 *ts_end = 100*(se + 60*(me + 60*he)) + *ts_end /10; 00201 return buf; 00202 } 00203 } 00204 return NULL; 00205 } 00206 00207 static int srt_decode_frame(AVCodecContext *avctx, 00208 void *data, int *got_sub_ptr, AVPacket *avpkt) 00209 { 00210 AVSubtitle *sub = data; 00211 int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1; 00212 char buffer[2048]; 00213 const char *ptr = avpkt->data; 00214 const char *end = avpkt->data + avpkt->size; 00215 00216 if (avpkt->size <= 0) 00217 return avpkt->size; 00218 00219 ff_ass_init(sub); 00220 00221 while (ptr < end && *ptr) { 00222 ptr = read_ts(ptr, &ts_start, &ts_end, &x1, &y1, &x2, &y2); 00223 if (!ptr) 00224 break; 00225 ptr = srt_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr, 00226 x1, y1, x2, y2); 00227 ff_ass_add_rect(sub, buffer, ts_start, ts_end, 0); 00228 } 00229 00230 *got_sub_ptr = sub->num_rects > 0; 00231 return avpkt->size; 00232 } 00233 00234 AVCodec ff_srt_decoder = { 00235 .name = "srt", 00236 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"), 00237 .type = AVMEDIA_TYPE_SUBTITLE, 00238 .id = CODEC_ID_SRT, 00239 .init = ff_ass_subtitle_header_default, 00240 .decode = srt_decode_frame, 00241 };