• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/ass_split.c

Go to the documentation of this file.
00001 /*
00002  * SSA/ASS spliting functions
00003  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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         /* convert V4 Style alignment to V4+ Style */
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             /* skip comments */
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 + 1);
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") + 1;
00271     }
00272     return buf;
00273 }
00274 
00275 static int ass_split(ASSSplitContext *ctx, const char *buf)
00276 {
00277     char c, section[16];
00278     int i;
00279 
00280     if (ctx->current_section >= 0)
00281         buf = ass_split_section(ctx, buf);
00282 
00283     while (buf && *buf) {
00284         if (sscanf(buf, "[%15[0-9A-Za-z+ ]]%c", section, &c) == 2) {
00285             buf += strcspn(buf, "\n") + 1;
00286             for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
00287                 if (!strcmp(section, ass_sections[i].section)) {
00288                     ctx->current_section = i;
00289                     buf = ass_split_section(ctx, buf);
00290                 }
00291         } else
00292             buf += strcspn(buf, "\n") + 1;
00293     }
00294     return buf ? 0 : AVERROR_INVALIDDATA;
00295 }
00296 
00297 ASSSplitContext *ff_ass_split(const char *buf)
00298 {
00299     ASSSplitContext *ctx = av_mallocz(sizeof(*ctx));
00300     ctx->current_section = -1;
00301     if (ass_split(ctx, buf) < 0) {
00302         ff_ass_split_free(ctx);
00303         return NULL;
00304     }
00305     return ctx;
00306 }
00307 
00308 static void free_section(ASSSplitContext *ctx, const ASSSection *section)
00309 {
00310     uint8_t *ptr = (uint8_t *)&ctx->ass + section->offset;
00311     int i, j, *count, c = 1;
00312 
00313     if (section->format_header) {
00314         ptr   = *(void **)ptr;
00315         count = (int *)((uint8_t *)&ctx->ass + section->offset_count);
00316     } else
00317         count = &c;
00318 
00319     if (ptr)
00320         for (i=0; i<*count; i++, ptr += section->size)
00321             for (j=0; section->fields[j].name; j++) {
00322                 const ASSFields *field = &section->fields[j];
00323                 if (field->type == ASS_STR)
00324                     av_freep(ptr + field->offset);
00325             }
00326     *count = 0;
00327 
00328     if (section->format_header)
00329         av_freep((uint8_t *)&ctx->ass + section->offset);
00330 }
00331 
00332 ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
00333                                int cache, int *number)
00334 {
00335     ASSDialog *dialog = NULL;
00336     int i, count;
00337     if (!cache)
00338         for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
00339             if (!strcmp(ass_sections[i].section, "Events")) {
00340                 free_section(ctx, &ass_sections[i]);
00341                 break;
00342             }
00343     count = ctx->ass.dialogs_count;
00344     if (ass_split(ctx, buf) == 0)
00345         dialog = ctx->ass.dialogs + count;
00346     if (number)
00347         *number = ctx->ass.dialogs_count - count;
00348     return dialog;
00349 }
00350 
00351 void ff_ass_split_free(ASSSplitContext *ctx)
00352 {
00353     if (ctx) {
00354         int i;
00355         for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
00356             free_section(ctx, &ass_sections[i]);
00357         av_free(ctx);
00358     }
00359 }
00360 
00361 
00362 int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
00363                                 const char *buf)
00364 {
00365     const char *text = NULL;
00366     char new_line[2];
00367     int text_len = 0;
00368 
00369     while (buf && *buf) {
00370         if (text && callbacks->text &&
00371             (sscanf(buf, "\\%1[nN]", new_line) == 1 ||
00372              !strncmp(buf, "{\\", 2))) {
00373             callbacks->text(priv, text, text_len);
00374             text = NULL;
00375         }
00376         if (sscanf(buf, "\\%1[nN]", new_line) == 1) {
00377             if (callbacks->new_line)
00378                 callbacks->new_line(priv, new_line[0] == 'N');
00379             buf += 2;
00380         } else if (!strncmp(buf, "{\\", 2)) {
00381             buf++;
00382             while (*buf == '\\') {
00383                 char style[2], c[2], sep[2], c_num[2] = "0", tmp[128] = {0};
00384                 unsigned int color = 0xFFFFFFFF;
00385                 int len, size = -1, an = -1, alpha = -1;
00386                 int x1, y1, x2, y2, t1 = -1, t2 = -1;
00387                 if (sscanf(buf, "\\%1[bisu]%1[01\\}]%n", style, c, &len) > 1) {
00388                     int close = c[0] == '0' ? 1 : c[0] == '1' ? 0 : -1;
00389                     len += close != -1;
00390                     if (callbacks->style)
00391                         callbacks->style(priv, style[0], close);
00392                 } else if (sscanf(buf, "\\c%1[\\}]%n", sep, &len) > 0 ||
00393                            sscanf(buf, "\\c&H%X&%1[\\}]%n", &color, sep, &len) > 1 ||
00394                            sscanf(buf, "\\%1[1234]c%1[\\}]%n", c_num, sep, &len) > 1 ||
00395                            sscanf(buf, "\\%1[1234]c&H%X&%1[\\}]%n", c_num, &color, sep, &len) > 2) {
00396                     if (callbacks->color)
00397                         callbacks->color(priv, color, c_num[0] - '0');
00398                 } else if (sscanf(buf, "\\alpha%1[\\}]%n", sep, &len) > 0 ||
00399                            sscanf(buf, "\\alpha&H%2X&%1[\\}]%n", &alpha, sep, &len) > 1 ||
00400                            sscanf(buf, "\\%1[1234]a%1[\\}]%n", c_num, sep, &len) > 1 ||
00401                            sscanf(buf, "\\%1[1234]a&H%2X&%1[\\}]%n", c_num, &alpha, sep, &len) > 2) {
00402                     if (callbacks->alpha)
00403                         callbacks->alpha(priv, alpha, c_num[0] - '0');
00404                 } else if (sscanf(buf, "\\fn%1[\\}]%n", sep, &len) > 0 ||
00405                            sscanf(buf, "\\fn%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
00406                     if (callbacks->font_name)
00407                         callbacks->font_name(priv, tmp[0] ? tmp : NULL);
00408                 } else if (sscanf(buf, "\\fs%1[\\}]%n", sep, &len) > 0 ||
00409                            sscanf(buf, "\\fs%u%1[\\}]%n", &size, sep, &len) > 1) {
00410                     if (callbacks->font_size)
00411                         callbacks->font_size(priv, size);
00412                 } else if (sscanf(buf, "\\a%1[\\}]%n", sep, &len) > 0 ||
00413                            sscanf(buf, "\\a%2u%1[\\}]%n", &an, sep, &len) > 1 ||
00414                            sscanf(buf, "\\an%1[\\}]%n", sep, &len) > 0 ||
00415                            sscanf(buf, "\\an%1u%1[\\}]%n", &an, sep, &len) > 1) {
00416                     if (an != -1 && buf[2] != 'n')
00417                         an = (an&3) + (an&4 ? 6 : an&8 ? 3 : 0);
00418                     if (callbacks->alignment)
00419                         callbacks->alignment(priv, an);
00420                 } else if (sscanf(buf, "\\r%1[\\}]%n", sep, &len) > 0 ||
00421                            sscanf(buf, "\\r%127[^\\}]%1[\\}]%n", tmp, sep, &len) > 1) {
00422                     if (callbacks->cancel_overrides)
00423                         callbacks->cancel_overrides(priv, tmp);
00424                 } else if (sscanf(buf, "\\move(%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, sep, &len) > 4 ||
00425                            sscanf(buf, "\\move(%d,%d,%d,%d,%d,%d)%1[\\}]%n", &x1, &y1, &x2, &y2, &t1, &t2, sep, &len) > 6) {
00426                     if (callbacks->move)
00427                         callbacks->move(priv, x1, y1, x2, y2, t1, t2);
00428                 } else if (sscanf(buf, "\\pos(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
00429                     if (callbacks->move)
00430                         callbacks->move(priv, x1, y1, x1, y1, -1, -1);
00431                 } else if (sscanf(buf, "\\org(%d,%d)%1[\\}]%n", &x1, &y1, sep, &len) > 2) {
00432                     if (callbacks->origin)
00433                         callbacks->origin(priv, x1, y1);
00434                 } else {
00435                     len = strcspn(buf+1, "\\}") + 2;  /* skip unknown code */
00436                 }
00437                 buf += len - 1;
00438             }
00439             if (*buf++ != '}')
00440                 return AVERROR_INVALIDDATA;
00441         } else {
00442             if (!text) {
00443                 text = buf;
00444                 text_len = 1;
00445             } else
00446                 text_len++;
00447             buf++;
00448         }
00449     }
00450     if (text && callbacks->text)
00451         callbacks->text(priv, text, text_len);
00452     if (callbacks->end)
00453         callbacks->end(priv);
00454     return 0;
00455 }
00456 
00457 ASSStyle *ass_style_get(ASSSplitContext *ctx, const char *style)
00458 {
00459     ASS *ass = &ctx->ass;
00460     int i;
00461 
00462     if (!style || !*style)
00463         style = "Default";
00464     for (i=0; i<ass->styles_count; i++)
00465         if (!strcmp(ass->styles[i].name, style))
00466             return ass->styles + i;
00467     return NULL;
00468 }

Generated on Fri Feb 22 2013 07:24:25 for FFmpeg by  doxygen 1.7.1