00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "ass_split.h"
00024
00025 typedef enum {
00026 ASS_STR,
00027 ASS_INT,
00028 ASS_FLT,
00029 ASS_COLOR,
00030 ASS_TIMESTAMP,
00031 ASS_ALGN,
00032 } ASSFieldType;
00033
00034 typedef struct {
00035 const char *name;
00036 int type;
00037 int offset;
00038 } ASSFields;
00039
00040 typedef struct {
00041 const char *section;
00042 const char *format_header;
00043 const char *fields_header;
00044 int size;
00045 int offset;
00046 int offset_count;
00047 ASSFields fields[10];
00048 } ASSSection;
00049
00050 static const ASSSection ass_sections[] = {
00051 { .section = "Script Info",
00052 .offset = offsetof(ASS, script_info),
00053 .fields = {{"ScriptType", ASS_STR, offsetof(ASSScriptInfo, script_type)},
00054 {"Collisions", ASS_STR, offsetof(ASSScriptInfo, collisions) },
00055 {"PlayResX", ASS_INT, offsetof(ASSScriptInfo, play_res_x) },
00056 {"PlayResY", ASS_INT, offsetof(ASSScriptInfo, play_res_y) },
00057 {"Timer", ASS_FLT, offsetof(ASSScriptInfo, timer) },
00058 {0},
00059 }
00060 },
00061 { .section = "V4+ Styles",
00062 .format_header = "Format",
00063 .fields_header = "Style",
00064 .size = sizeof(ASSStyle),
00065 .offset = offsetof(ASS, styles),
00066 .offset_count = offsetof(ASS, styles_count),
00067 .fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
00068 {"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
00069 {"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
00070 {"PrimaryColour",ASS_COLOR,offsetof(ASSStyle, primary_color)},
00071 {"BackColour", ASS_COLOR,offsetof(ASSStyle, back_color) },
00072 {"Bold", ASS_INT, offsetof(ASSStyle, bold) },
00073 {"Italic", ASS_INT, offsetof(ASSStyle, italic) },
00074 {"Underline", ASS_INT, offsetof(ASSStyle, underline) },
00075 {"Alignment", ASS_INT, offsetof(ASSStyle, alignment) },
00076 {0},
00077 }
00078 },
00079 { .section = "V4 Styles",
00080 .format_header = "Format",
00081 .fields_header = "Style",
00082 .size = sizeof(ASSStyle),
00083 .offset = offsetof(ASS, styles),
00084 .offset_count = offsetof(ASS, styles_count),
00085 .fields = {{"Name", ASS_STR, offsetof(ASSStyle, name) },
00086 {"Fontname", ASS_STR, offsetof(ASSStyle, font_name) },
00087 {"Fontsize", ASS_INT, offsetof(ASSStyle, font_size) },
00088 {"PrimaryColour",ASS_COLOR,offsetof(ASSStyle, primary_color)},
00089 {"BackColour", ASS_COLOR,offsetof(ASSStyle, back_color) },
00090 {"Bold", ASS_INT, offsetof(ASSStyle, bold) },
00091 {"Italic", ASS_INT, offsetof(ASSStyle, italic) },
00092 {"Alignment", ASS_ALGN, offsetof(ASSStyle, alignment) },
00093 {0},
00094 }
00095 },
00096 { .section = "Events",
00097 .format_header = "Format",
00098 .fields_header = "Dialogue",
00099 .size = sizeof(ASSDialog),
00100 .offset = offsetof(ASS, dialogs),
00101 .offset_count = offsetof(ASS, dialogs_count),
00102 .fields = {{"Layer", ASS_INT, offsetof(ASSDialog, layer) },
00103 {"Start", ASS_TIMESTAMP, offsetof(ASSDialog, start) },
00104 {"End", ASS_TIMESTAMP, offsetof(ASSDialog, end) },
00105 {"Style", ASS_STR, offsetof(ASSDialog, style) },
00106 {"Text", ASS_STR, offsetof(ASSDialog, text) },
00107 {0},
00108 }
00109 },
00110 };
00111
00112
00113 typedef int (*ASSConvertFunc)(void *dest, const char *buf, int len);
00114
00115 static int convert_str(void *dest, const char *buf, int len)
00116 {
00117 char *str = av_malloc(len + 1);
00118 if (str) {
00119 memcpy(str, buf, len);
00120 str[len] = 0;
00121 if (*(void **)dest)
00122 av_free(*(void **)dest);
00123 *(char **)dest = str;
00124 }
00125 return !str;
00126 }
00127 static int convert_int(void *dest, const char *buf, int len)
00128 {
00129 return sscanf(buf, "%d", (int *)dest) == 1;
00130 }
00131 static int convert_flt(void *dest, const char *buf, int len)
00132 {
00133 return sscanf(buf, "%f", (float *)dest) == 1;
00134 }
00135 static int convert_color(void *dest, const char *buf, int len)
00136 {
00137 return sscanf(buf, "&H%8x", (int *)dest) == 1 ||
00138 sscanf(buf, "%d", (int *)dest) == 1;
00139 }
00140 static int convert_timestamp(void *dest, const char *buf, int len)
00141 {
00142 int c, h, m, s, cs;
00143 if ((c = sscanf(buf, "%d:%02d:%02d.%02d", &h, &m, &s, &cs)) == 4)
00144 *(int *)dest = 360000*h + 6000*m + 100*s + cs;
00145 return c == 4;
00146 }
00147 static int convert_alignment(void *dest, const char *buf, int len)
00148 {
00149 int a;
00150 if (sscanf(buf, "%d", &a) == 1) {
00151
00152 *(int *)dest = a + ((a&4) >> 1) - 5*!!(a&8);
00153 return 1;
00154 }
00155 return 0;
00156 }
00157
00158 static const ASSConvertFunc convert_func[] = {
00159 [ASS_STR] = convert_str,
00160 [ASS_INT] = convert_int,
00161 [ASS_FLT] = convert_flt,
00162 [ASS_COLOR] = convert_color,
00163 [ASS_TIMESTAMP] = convert_timestamp,
00164 [ASS_ALGN] = convert_alignment,
00165 };
00166
00167
00168 struct ASSSplitContext {
00169 ASS ass;
00170 int current_section;
00171 int field_number[FF_ARRAY_ELEMS(ass_sections)];
00172 int *field_order[FF_ARRAY_ELEMS(ass_sections)];
00173 };
00174
00175
00176 static uint8_t *realloc_section_array(ASSSplitContext *ctx)
00177 {
00178 const ASSSection *section = &ass_sections[ctx->current_section];
00179 int *count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
00180 void **section_ptr = (void **)((uint8_t *)&ctx->ass + section->offset);
00181 uint8_t *tmp = av_realloc(*section_ptr, (*count+1)*section->size);
00182 if (!tmp)
00183 return NULL;
00184 *section_ptr = tmp;
00185 tmp += *count * section->size;
00186 memset(tmp, 0, section->size);
00187 (*count)++;
00188 return tmp;
00189 }
00190
00191 static inline int is_eol(char buf)
00192 {
00193 return buf == '\r' || buf == '\n' || buf == 0;
00194 }
00195
00196 static inline const char *skip_space(const char *buf)
00197 {
00198 while (*buf == ' ')
00199 buf++;
00200 return buf;
00201 }
00202
00203 static const char *ass_split_section(ASSSplitContext *ctx, const char *buf)
00204 {
00205 const ASSSection *section = &ass_sections[ctx->current_section];
00206 int *number = &ctx->field_number[ctx->current_section];
00207 int *order = ctx->field_order[ctx->current_section];
00208 int *tmp, i, len;
00209
00210 while (buf && *buf) {
00211 if (buf[0] == '[') {
00212 ctx->current_section = -1;
00213 break;
00214 }
00215 if (buf[0] == ';' || (buf[0] == '!' && buf[1] == ':')) {
00216
00217 } else if (section->format_header && !order) {
00218 len = strlen(section->format_header);
00219 if (strncmp(buf, section->format_header, len) || buf[len] != ':')
00220 return NULL;
00221 buf += len + 1;
00222 while (!is_eol(*buf)) {
00223 buf = skip_space(buf);
00224 len = strcspn(buf, ", \r\n");
00225 if (!(tmp = av_realloc(order, (*number + 1) * sizeof(*order))))
00226 return NULL;
00227 order = tmp;
00228 order[*number] = -1;
00229 for (i=0; section->fields[i].name; i++)
00230 if (!strncmp(buf, section->fields[i].name, len)) {
00231 order[*number] = i;
00232 break;
00233 }
00234 (*number)++;
00235 buf = skip_space(buf + len + (buf[len] == ','));
00236 }
00237 ctx->field_order[ctx->current_section] = order;
00238 } else if (section->fields_header) {
00239 len = strlen(section->fields_header);
00240 if (!strncmp(buf, section->fields_header, len) && buf[len] == ':') {
00241 uint8_t *ptr, *struct_ptr = realloc_section_array(ctx);
00242 if (!struct_ptr) return NULL;
00243 buf += len + 1;
00244 for (i=0; !is_eol(*buf) && i < *number; i++) {
00245 int last = i == *number - 1;
00246 buf = skip_space(buf);
00247 len = strcspn(buf, last ? "\r\n" : ",\r\n");
00248 if (order[i] >= 0) {
00249 ASSFieldType type = section->fields[order[i]].type;
00250 ptr = struct_ptr + section->fields[order[i]].offset;
00251 convert_func[type](ptr, buf, len);
00252 }
00253 buf = skip_space(buf + len + !last);
00254 }
00255 }
00256 } else {
00257 len = strcspn(buf, ":\r\n");
00258 if (buf[len] == ':') {
00259 for (i=0; section->fields[i].name; i++)
00260 if (!strncmp(buf, section->fields[i].name, len)) {
00261 ASSFieldType type = section->fields[i].type;
00262 uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
00263 ptr += section->fields[i].offset;
00264 buf = skip_space(buf + len + 1);
00265 convert_func[type](ptr, buf, strcspn(buf, "\r\n"));
00266 break;
00267 }
00268 }
00269 }
00270 buf += strcspn(buf, "\n");
00271 buf += !!*buf;
00272 }
00273 return buf;
00274 }
00275
00276 static int ass_split(ASSSplitContext *ctx, const char *buf)
00277 {
00278 char c, section[16];
00279 int i;
00280
00281 if (ctx->current_section >= 0)
00282 buf = ass_split_section(ctx, buf);
00283
00284 while (buf && *buf) {
00285 if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) {
00286 buf += strcspn(buf, "\n") + 1;
00287 for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
00288 if (!strcmp(section, ass_sections[i].section)) {
00289 ctx->current_section = i;
00290 buf = ass_split_section(ctx, buf);
00291 }
00292 } else
00293 buf += strcspn(buf, "\n") + 1;
00294 }
00295 return buf ? 0 : AVERROR_INVALIDDATA;
00296 }
00297
00298 ASSSplitContext *ff_ass_split(const char *buf)
00299 {
00300 ASSSplitContext *ctx = av_mallocz(sizeof(*ctx));
00301 ctx->current_section = -1;
00302 if (ass_split(ctx, buf) < 0) {
00303 ff_ass_split_free(ctx);
00304 return NULL;
00305 }
00306 return ctx;
00307 }
00308
00309 static void free_section(ASSSplitContext *ctx, const ASSSection *section)
00310 {
00311 uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
00312 int i, j, *count, c = 1;
00313
00314 if (section->format_header) {
00315 ptr = *(void **)ptr;
00316 count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
00317 } else
00318 count = &c;
00319
00320 if (ptr)
00321 for (i=0; i<*count; i++, ptr += section->size)
00322 for (j=0; section->fields[j].name; j++) {
00323 const ASSFields *field = §ion->fields[j];
00324 if (field->type == ASS_STR)
00325 av_freep(ptr + field->offset);
00326 }
00327 *count = 0;
00328
00329 if (section->format_header)
00330 av_freep((uint8_t *)&ctx->ass + section->offset);
00331 }
00332
00333 ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
00334 int cache, int *number)
00335 {
00336 ASSDialog *dialog = NULL;
00337 int i, count;
00338 if (!cache)
00339 for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
00340 if (!strcmp(ass_sections[i].section, "Events")) {
00341 free_section(ctx, &ass_sections[i]);
00342 break;
00343 }
00344 count = ctx->ass.dialogs_count;
00345 if (ass_split(ctx, buf) == 0)
00346 dialog = ctx->ass.dialogs + count;
00347 if (number)
00348 *number = ctx->ass.dialogs_count - count;
00349 return dialog;
00350 }
00351
00352 void ff_ass_split_free(ASSSplitContext *ctx)
00353 {
00354 if (ctx) {
00355 int i;
00356 for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++) {
00357 free_section(ctx, &ass_sections[i]);
00358 av_freep(&(ctx->field_order[i]));
00359 }
00360 av_free(ctx);
00361 }
00362 }
00363
00364
00365 int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
00366 const char *buf)
00367 {
00368 const char *text = NULL;
00369 char new_line[2];
00370 int text_len = 0;
00371
00372 while (*buf) {
00373 if (text && callbacks->text &&
00374 (sscanf(buf, "\\%1[nN]", new_line) == 1 ||
00375 !strncmp(buf, "{\\", 2))) {
00376 callbacks->text(priv, text, text_len);
00377 text = NULL;
00378 }
00379 if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
00380 if (callbacks->new_line)
00381 callbacks->new_line(priv, new_line[0] == 'N');
00382 buf += 2;
00383 } else if (!strncmp(buf, "{\\", 2)) {
00384 buf++;
00385 while (*buf == '\\') {
00386 char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
00387 unsigned int color = 0xFFFFFFFF;
00388 int len, size = -1, an = -1, alpha = -1;
00389 int x1, y1, x2, y2, t1 = -1, t2 = -1;
00390 if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
00391 int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
00392 len += close != -1;
00393 if (callbacks->style)
00394 callbacks->style(priv, style[0], close);
00395 } else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
00396 sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
00397 sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
00398 sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
00399 if (callbacks->color)
00400 callbacks->color(priv, color, c_num[0] - '0');
00401 } else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
00402 sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
00403 sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
00404 sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
00405 if (callbacks->alpha)
00406 callbacks->alpha(priv, alpha, c_num[0] - '0');
00407 } else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
00408 sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
00409 if (callbacks->font_name)
00410 callbacks->font_name(priv, tmp[0] ? tmp : NULL);
00411 } else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
00412 sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
00413 if (callbacks->font_size)
00414 callbacks->font_size(priv, size);
00415 } else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
00416 sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
00417 sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
00418 sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
00419 if (an != -1 && buf[2] != 'n')
00420 an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
00421 if (callbacks->alignment)
00422 callbacks->alignment(priv, an);
00423 } else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
00424 sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
00425 if (callbacks->cancel_overrides)
00426 callbacks->cancel_overrides(priv, tmp);
00427 } else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
00428 sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
00429 if (callbacks->move)
00430 callbacks->move(priv, x1, y1, x2, y2, t1, t2);
00431 } else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
00432 if (callbacks->move)
00433 callbacks->move(priv, x1, y1, x1, y1, -1, -1);
00434 } else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
00435 if (callbacks->origin)
00436 callbacks->origin(priv, x1, y1);
00437 } else {
00438 len = strcspn(buf+1, "\\}") + 2;
00439 }
00440 buf += len - 1;
00441 }
00442 if (*buf++ != '}')
00443 return AVERROR_INVALIDDATA;
00444 } else {
00445 if (!text) {
00446 text = buf;
00447 text_len = 1;
00448 } else
00449 text_len++;
00450 buf++;
00451 }
00452 }
00453 if (text && callbacks->text)
00454 callbacks->text(priv, text, text_len);
00455 if (callbacks->end)
00456 callbacks->end(priv);
00457 return 0;
00458 }
00459
00460 ASSStyle *ff_ass_style_get(ASSSplitContext *ctx, const char *style)
00461 {
00462 ASS *ass = &ctx->ass;
00463 int i;
00464
00465 if (!style || !*style)
00466 style = "Default";
00467 for (i=0; i<ass->styles_count; i++)
00468 if (!strcmp(ass->styles[i].name, style))
00469 return ass->styles + i;
00470 return NULL;
00471 }