00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "libavutil/common.h"
00024 #include "libavutil/intreadwrite.h"
00025 #include "libavutil/parseutils.h"
00026 #include "avcodec.h"
00027 #include "ass.h"
00028
00029 static int html_color_parse(AVCodecContext *avctx, const char *str)
00030 {
00031 uint8_t rgba[4];
00032 if (av_parse_color(rgba, str, strcspn(str, "\" >"), avctx) < 0)
00033 return -1;
00034 return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
00035 }
00036
00037 enum {
00038 PARAM_UNKNOWN = -1,
00039 PARAM_SIZE,
00040 PARAM_COLOR,
00041 PARAM_FACE,
00042 PARAM_NUMBER
00043 };
00044
00045 typedef struct {
00046 char tag[128];
00047 char param[PARAM_NUMBER][128];
00048 } SrtStack;
00049
00050 static const char *srt_to_ass(AVCodecContext *avctx, char *out, char *out_end,
00051 const char *in, int x1, int y1, int x2, int y2)
00052 {
00053 char c, *param, buffer[128], tmp[128];
00054 int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
00055 SrtStack stack[16];
00056
00057 stack[0].tag[0] = 0;
00058 strcpy(stack[0].param[PARAM_SIZE], "{\\fs}");
00059 strcpy(stack[0].param[PARAM_COLOR], "{\\c}");
00060 strcpy(stack[0].param[PARAM_FACE], "{\\fn}");
00061
00062 if (x1 >= 0 && y1 >= 0) {
00063 if (x2 >= 0 && y2 >= 0 && (x2 != x1 || y2 != y1))
00064 snprintf(out, out_end-out,
00065 "{\\an1}{\\move(%d,%d,%d,%d)}", x1, y1, x2, y2);
00066 else
00067 snprintf(out, out_end-out, "{\\an1}{\\pos(%d,%d)}", x1, y1);
00068 out += strlen(out);
00069 }
00070
00071 for (; out < out_end && !end && *in; in++) {
00072 switch (*in) {
00073 case '\r':
00074 break;
00075 case '\n':
00076 if (line_start) {
00077 end = 1;
00078 break;
00079 }
00080 while (out[-1] == ' ')
00081 out--;
00082 snprintf(out, out_end-out, "\\N");
00083 if(out<out_end) out += strlen(out);
00084 line_start = 1;
00085 break;
00086 case ' ':
00087 if (!line_start)
00088 *out++ = *in;
00089 break;
00090 case '{':
00091
00092 an += sscanf(in, "{\\an%*1u}%c", &c) == 1;
00093 if ((an != 1 && sscanf(in, "{\\%*[^}]}%n%c", &len, &c) > 0) ||
00094 sscanf(in, "{%*1[CcFfoPSsYy]:%*[^}]}%n%c", &len, &c) > 0) {
00095 in += len - 1;
00096 } else
00097 *out++ = *in;
00098 break;
00099 case '<':
00100 tag_close = in[1] == '/';
00101 if (sscanf(in+tag_close+1, "%127[^>]>%n%c", buffer, &len,&c) >= 2) {
00102 if ((param = strchr(buffer, ' ')))
00103 *param++ = 0;
00104 if ((!tag_close && sptr < FF_ARRAY_ELEMS(stack)) ||
00105 ( tag_close && sptr > 0 && !strcmp(stack[sptr-1].tag, buffer))) {
00106 int i, j, unknown = 0;
00107 in += len + tag_close;
00108 if (!tag_close)
00109 memset(stack+sptr, 0, sizeof(*stack));
00110 if (!strcmp(buffer, "font")) {
00111 if (tag_close) {
00112 for (i=PARAM_NUMBER-1; i>=0; i--)
00113 if (stack[sptr-1].param[i][0])
00114 for (j=sptr-2; j>=0; j--)
00115 if (stack[j].param[i][0]) {
00116 snprintf(out, out_end-out,
00117 "%s", stack[j].param[i]);
00118 if(out<out_end) out += strlen(out);
00119 break;
00120 }
00121 } else {
00122 while (param) {
00123 if (!strncmp(param, "size=", 5)) {
00124 unsigned font_size;
00125 param += 5 + (param[5] == '"');
00126 if (sscanf(param, "%u", &font_size) == 1) {
00127 snprintf(stack[sptr].param[PARAM_SIZE],
00128 sizeof(stack[0].param[PARAM_SIZE]),
00129 "{\\fs%u}", font_size);
00130 }
00131 } else if (!strncmp(param, "color=", 6)) {
00132 param += 6 + (param[6] == '"');
00133 snprintf(stack[sptr].param[PARAM_COLOR],
00134 sizeof(stack[0].param[PARAM_COLOR]),
00135 "{\\c&H%X&}",
00136 html_color_parse(avctx, param));
00137 } else if (!strncmp(param, "face=", 5)) {
00138 param += 5 + (param[5] == '"');
00139 len = strcspn(param,
00140 param[-1] == '"' ? "\"" :" ");
00141 av_strlcpy(tmp, param,
00142 FFMIN(sizeof(tmp), len+1));
00143 param += len;
00144 snprintf(stack[sptr].param[PARAM_FACE],
00145 sizeof(stack[0].param[PARAM_FACE]),
00146 "{\\fn%s}", tmp);
00147 }
00148 if ((param = strchr(param, ' ')))
00149 param++;
00150 }
00151 for (i=0; i<PARAM_NUMBER; i++)
00152 if (stack[sptr].param[i][0]) {
00153 snprintf(out, out_end-out,
00154 "%s", stack[sptr].param[i]);
00155 if(out<out_end) out += strlen(out);
00156 }
00157 }
00158 } else if (!buffer[1] && strspn(buffer, "bisu") == 1) {
00159 snprintf(out, out_end-out,
00160 "{\\%c%d}", buffer[0], !tag_close);
00161 if(out<out_end) out += strlen(out);
00162 } else {
00163 unknown = 1;
00164 snprintf(tmp, sizeof(tmp), "</%s>", buffer);
00165 }
00166 if (tag_close) {
00167 sptr--;
00168 } else if (unknown && !strstr(in, tmp)) {
00169 in -= len + tag_close;
00170 *out++ = *in;
00171 } else
00172 av_strlcpy(stack[sptr++].tag, buffer,
00173 sizeof(stack[0].tag));
00174 break;
00175 }
00176 }
00177 default:
00178 *out++ = *in;
00179 break;
00180 }
00181 if (*in != ' ' && *in != '\r' && *in != '\n')
00182 line_start = 0;
00183 }
00184
00185 out = FFMIN(out, out_end-3);
00186 while (!strncmp(out-2, "\\N", 2))
00187 out -= 2;
00188 while (out[-1] == ' ')
00189 out--;
00190 snprintf(out, out_end-out, "\r\n");
00191 return in;
00192 }
00193
00194 static const char *read_ts(const char *buf, int *ts_start, int *ts_end,
00195 int *x1, int *y1, int *x2, int *y2)
00196 {
00197 int i, hs, ms, ss, he, me, se;
00198
00199 for (i=0; i<2; i++) {
00200
00201 int c = sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
00202 "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
00203 &hs, &ms, &ss, ts_start, &he, &me, &se, ts_end,
00204 x1, x2, y1, y2);
00205 buf += strcspn(buf, "\n") + 1;
00206 if (c >= 8) {
00207 *ts_start = 100*(ss + 60*(ms + 60*hs)) + *ts_start/10;
00208 *ts_end = 100*(se + 60*(me + 60*he)) + *ts_end /10;
00209 return buf;
00210 }
00211 }
00212 return NULL;
00213 }
00214
00215 static int srt_decode_frame(AVCodecContext *avctx,
00216 void *data, int *got_sub_ptr, AVPacket *avpkt)
00217 {
00218 AVSubtitle *sub = data;
00219 int ts_start, ts_end, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
00220 char buffer[2048];
00221 const char *ptr = avpkt->data;
00222 const char *end = avpkt->data + avpkt->size;
00223 int size;
00224 const uint8_t *p = av_packet_get_side_data(avpkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
00225
00226 if (p && size == 16) {
00227 x1 = AV_RL32(p );
00228 y1 = AV_RL32(p + 4);
00229 x2 = AV_RL32(p + 8);
00230 y2 = AV_RL32(p + 12);
00231 }
00232
00233 if (avpkt->size <= 0)
00234 return avpkt->size;
00235
00236 while (ptr < end && *ptr) {
00237 if (avctx->codec->id == AV_CODEC_ID_SRT) {
00238 ptr = read_ts(ptr, &ts_start, &ts_end, &x1, &y1, &x2, &y2);
00239 if (!ptr)
00240 break;
00241 } else {
00242
00243 ts_start = av_rescale_q(avpkt->pts,
00244 avctx->time_base,
00245 (AVRational){1,100});
00246 ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
00247 avctx->time_base,
00248 (AVRational){1,100});
00249 }
00250 ptr = srt_to_ass(avctx, buffer, buffer+sizeof(buffer), ptr,
00251 x1, y1, x2, y2);
00252 ff_ass_add_rect(sub, buffer, ts_start, ts_end-ts_start, 0);
00253 }
00254
00255 *got_sub_ptr = sub->num_rects > 0;
00256 return avpkt->size;
00257 }
00258
00259 #if CONFIG_SRT_DECODER
00260
00261 AVCodec ff_srt_decoder = {
00262 .name = "srt",
00263 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle with embedded timing"),
00264 .type = AVMEDIA_TYPE_SUBTITLE,
00265 .id = AV_CODEC_ID_SRT,
00266 .init = ff_ass_subtitle_header_default,
00267 .decode = srt_decode_frame,
00268 };
00269 #endif
00270
00271 #if CONFIG_SUBRIP_DECODER
00272 AVCodec ff_subrip_decoder = {
00273 .name = "subrip",
00274 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
00275 .type = AVMEDIA_TYPE_SUBTITLE,
00276 .id = AV_CODEC_ID_SUBRIP,
00277 .init = ff_ass_subtitle_header_default,
00278 .decode = srt_decode_frame,
00279 };
00280 #endif