00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include <time.h>
00028 #include "ass.h"
00029 #include "jacosub.h"
00030 #include "libavutil/avstring.h"
00031 #include "libavutil/bprint.h"
00032
00033 #undef time
00034
00035 static int insert_text(AVBPrint *dst, const char *in, const char *arg)
00036 {
00037 av_bprintf(dst, "%s", arg);
00038 return 0;
00039 }
00040
00041 static int insert_datetime(AVBPrint *dst, const char *in, const char *arg)
00042 {
00043 char buf[16] = {0};
00044 time_t now = time(0);
00045 struct tm ltime;
00046
00047 #if HAVE_LOCALTIME_R
00048 localtime_r(&now, <ime);
00049 #else
00050 ltime = *localtime(&now);
00051 #endif
00052 strftime(buf, sizeof(buf), arg, <ime);
00053 av_bprintf(dst, "%s", buf);
00054 return 0;
00055 }
00056
00057 static int insert_color(AVBPrint *dst, const char *in, const char *arg)
00058 {
00059 return 1;
00060 }
00061
00062 static int insert_font(AVBPrint *dst, const char *in, const char *arg)
00063 {
00064 return 1;
00065 }
00066
00067 static const struct {
00068 const char *from;
00069 const char *arg;
00070 int (*func)(AVBPrint *dst, const char *in, const char *arg);
00071 } ass_codes_map[] = {
00072 {"\\~", "~", insert_text},
00073 {"~", "{\\h}", insert_text},
00074 {"\\n", "\\N", insert_text},
00075 {"\\D", "%d %b %Y", insert_datetime},
00076 {"\\T", "%H:%M", insert_datetime},
00077 {"\\N", "{\\r}", insert_text},
00078 {"\\I", "{\\i1}", insert_text},
00079 {"\\i", "{\\i0}", insert_text},
00080 {"\\B", "{\\b1}", insert_text},
00081 {"\\b", "{\\b0}", insert_text},
00082 {"\\U", "{\\u1}", insert_text},
00083 {"\\u", "{\\u0}", insert_text},
00084 {"\\C", "", insert_color},
00085 {"\\F", "", insert_font},
00086 };
00087
00088 enum {
00089 ALIGN_VB = 1<<0,
00090 ALIGN_VM = 1<<1,
00091 ALIGN_VT = 1<<2,
00092 ALIGN_JC = 1<<3,
00093 ALIGN_JL = 1<<4,
00094 ALIGN_JR = 1<<5,
00095 };
00096
00097 static void jacosub_to_ass(AVCodecContext *avctx, AVBPrint *dst, const char *src)
00098 {
00099 int i, valign = 0, halign = 0;
00100 char c = av_toupper(*src);
00101 char directives[128] = {0};
00102
00103
00104 if ((c >= 'A' && c <= 'Z') || c == '[') {
00105 char *p = directives;
00106 char *pend = directives + sizeof(directives) - 1;
00107
00108 do *p++ = av_toupper(*src++);
00109 while (*src && !jss_whitespace(*src) && p < pend);
00110 *p = 0;
00111 src = jss_skip_whitespace(src);
00112 }
00113
00114
00115 if (strstr(directives, "VB")) valign = ALIGN_VB;
00116 else if (strstr(directives, "VM")) valign = ALIGN_VM;
00117 else if (strstr(directives, "VT")) valign = ALIGN_VT;
00118 if (strstr(directives, "JC")) halign = ALIGN_JC;
00119 else if (strstr(directives, "JL")) halign = ALIGN_JL;
00120 else if (strstr(directives, "JR")) halign = ALIGN_JR;
00121 if (valign || halign) {
00122 if (!valign) valign = ALIGN_VB;
00123 if (!halign) halign = ALIGN_JC;
00124 switch (valign | halign) {
00125 case ALIGN_VB | ALIGN_JL: av_bprintf(dst, "{\\an1}"); break;
00126 case ALIGN_VB | ALIGN_JC: av_bprintf(dst, "{\\an2}"); break;
00127 case ALIGN_VB | ALIGN_JR: av_bprintf(dst, "{\\an3}"); break;
00128 case ALIGN_VM | ALIGN_JL: av_bprintf(dst, "{\\an4}"); break;
00129 case ALIGN_VM | ALIGN_JC: av_bprintf(dst, "{\\an5}"); break;
00130 case ALIGN_VM | ALIGN_JR: av_bprintf(dst, "{\\an6}"); break;
00131 case ALIGN_VT | ALIGN_JL: av_bprintf(dst, "{\\an7}"); break;
00132 case ALIGN_VT | ALIGN_JC: av_bprintf(dst, "{\\an8}"); break;
00133 case ALIGN_VT | ALIGN_JR: av_bprintf(dst, "{\\an9}"); break;
00134 }
00135 }
00136
00137
00138 while (*src && *src != '\n') {
00139
00140
00141 if (src[0] == '\\' && src[1] == '\n') {
00142 src += 2;
00143 while (jss_whitespace(*src))
00144 src++;
00145 continue;
00146 }
00147
00148
00149 for (i = 0; i < FF_ARRAY_ELEMS(ass_codes_map); i++) {
00150 const char *from = ass_codes_map[i].from;
00151 const char *arg = ass_codes_map[i].arg;
00152 size_t codemap_len = strlen(from);
00153
00154 if (!strncmp(src, from, codemap_len)) {
00155 src += codemap_len;
00156 src += ass_codes_map[i].func(dst, src, arg);
00157 break;
00158 }
00159 }
00160
00161
00162 if (i == FF_ARRAY_ELEMS(ass_codes_map))
00163 av_bprintf(dst, "%c", *src++);
00164 }
00165 av_bprintf(dst, "\r\n");
00166 }
00167
00168 static int jacosub_decode_frame(AVCodecContext *avctx,
00169 void *data, int *got_sub_ptr, AVPacket *avpkt)
00170 {
00171 AVSubtitle *sub = data;
00172 const char *ptr = avpkt->data;
00173
00174 if (avpkt->size <= 0)
00175 goto end;
00176
00177 if (*ptr) {
00178 AVBPrint buffer;
00179 char *dec_sub;
00180
00181
00182 ptr = jss_skip_whitespace(ptr);
00183 ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
00184 ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
00185
00186 av_bprint_init(&buffer, JSS_MAX_LINESIZE, JSS_MAX_LINESIZE);
00187 jacosub_to_ass(avctx, &buffer, ptr);
00188 av_bprint_finalize(&buffer, &dec_sub);
00189 ff_ass_add_rect(sub, dec_sub, avpkt->pts, avpkt->duration, 0);
00190 av_free(dec_sub);
00191 }
00192
00193 end:
00194 *got_sub_ptr = sub->num_rects > 0;
00195 return avpkt->size;
00196 }
00197
00198 AVCodec ff_jacosub_decoder = {
00199 .name = "jacosub",
00200 .long_name = NULL_IF_CONFIG_SMALL("JACOsub subtitle"),
00201 .type = AVMEDIA_TYPE_SUBTITLE,
00202 .id = AV_CODEC_ID_JACOSUB,
00203 .init = ff_ass_subtitle_header_default,
00204 .decode = jacosub_decode_frame,
00205 };