00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00028 #include "avformat.h"
00029 #include "internal.h"
00030 #include "libavcodec/jacosub.h"
00031 #include "libavutil/avstring.h"
00032 #include "libavutil/bprint.h"
00033 #include "libavutil/intreadwrite.h"
00034
00035 typedef struct {
00036 char *line;
00037 int64_t pos;
00038 int start;
00039 int end;
00040 } SubEntry;
00041
00042 typedef struct {
00043 int shift;
00044 unsigned timeres;
00045 SubEntry *subs;
00046 int nsub;
00047 int sid;
00048 } JACOsubContext;
00049
00050 static int timed_line(const char *ptr)
00051 {
00052 char c;
00053 return (sscanf(ptr, "%*u:%*u:%*u.%*u %*u:%*u:%*u.%*u %c", &c) == 1 ||
00054 sscanf(ptr, "@%*u @%*u %c", &c) == 1);
00055 }
00056
00057 static int jacosub_probe(AVProbeData *p)
00058 {
00059 const char *ptr = p->buf;
00060 const char *ptr_end = p->buf + p->buf_size;
00061
00062 if (AV_RB24(ptr) == 0xEFBBBF)
00063 ptr += 3;
00064
00065 while (ptr < ptr_end) {
00066 while (jss_whitespace(*ptr))
00067 ptr++;
00068 if (*ptr != '#' && *ptr != '\n') {
00069 if (timed_line(ptr))
00070 return AVPROBE_SCORE_MAX/2 + 1;
00071 return 0;
00072 }
00073 ptr += strcspn(ptr, "\n") + 1;
00074 }
00075 return 0;
00076 }
00077
00078 static const char * const cmds[] = {
00079 "CLOCKPAUSE",
00080 "DIRECTIVE",
00081 "FONT",
00082 "HRES",
00083 "INCLUDE",
00084 "PALETTE",
00085 "QUANTIZE",
00086 "RAMP",
00087 "SHIFT",
00088 "TIMERES",
00089 };
00090
00091 static int get_jss_cmd(char k)
00092 {
00093 int i;
00094
00095 k = av_toupper(k);
00096 for (i = 0; i < FF_ARRAY_ELEMS(cmds); i++)
00097 if (k == cmds[i][0])
00098 return i;
00099 return -1;
00100 }
00101
00102 static int jacosub_read_close(AVFormatContext *s)
00103 {
00104 int i;
00105 JACOsubContext *jacosub = s->priv_data;
00106
00107 for (i = 0; i < jacosub->nsub; i++)
00108 av_freep(&jacosub->subs[i].line);
00109 jacosub->nsub = 0;
00110 av_freep(&jacosub->subs);
00111 return 0;
00112 }
00113
00114 static const char *read_ts(JACOsubContext *jacosub, const char *buf,
00115 int *ts_start, int *ts_end)
00116 {
00117 int len;
00118 unsigned hs, ms, ss, fs;
00119 unsigned he, me, se, fe;
00120
00121
00122 if (sscanf(buf, "%u:%u:%u.%u %u:%u:%u.%u %n",
00123 &hs, &ms, &ss, &fs,
00124 &he, &me, &se, &fe, &len) == 8) {
00125 *ts_start = (hs*3600 + ms*60 + ss) * jacosub->timeres + fs;
00126 *ts_end = (he*3600 + me*60 + se) * jacosub->timeres + fe;
00127 goto shift_and_ret;
00128 }
00129
00130
00131 if (sscanf(buf, "@%u @%u %n", ts_start, ts_end, &len) == 2)
00132 goto shift_and_ret;
00133
00134 return NULL;
00135
00136 shift_and_ret:
00137 *ts_start = (*ts_start + jacosub->shift) * 100 / jacosub->timeres;
00138 *ts_end = (*ts_end + jacosub->shift) * 100 / jacosub->timeres;
00139 return buf + len;
00140 }
00141
00142 static int get_shift(int timeres, const char *buf)
00143 {
00144 int sign = 1;
00145 int a = 0, b = 0, c = 0, d = 0;
00146 #define SSEP "%*1[.:]"
00147 int n = sscanf(buf, "%d"SSEP"%d"SSEP"%d"SSEP"%d", &a, &b, &c, &d);
00148 #undef SSEP
00149
00150 if (*buf == '-' || a < 0) {
00151 sign = -1;
00152 a = FFABS(a);
00153 }
00154
00155 switch (n) {
00156 case 4: return sign * ((a*3600 + b*60 + c) * timeres + d);
00157 case 3: return sign * (( a*60 + b) * timeres + c);
00158 case 2: return sign * (( a) * timeres + b);
00159 }
00160
00161 return 0;
00162 }
00163
00164 static int cmp_timed_sub(const void *a, const void *b)
00165 {
00166 return ((const SubEntry*)a)->start - ((const SubEntry*)b)->start;
00167 }
00168
00169 static int jacosub_read_header(AVFormatContext *s)
00170 {
00171 AVBPrint header;
00172 AVIOContext *pb = s->pb;
00173 char line[JSS_MAX_LINESIZE];
00174 JACOsubContext *jacosub = s->priv_data;
00175 int shift_set = 0;
00176 int merge_line = 0;
00177 int i;
00178
00179 AVStream *st = avformat_new_stream(s, NULL);
00180 if (!st)
00181 return AVERROR(ENOMEM);
00182 avpriv_set_pts_info(st, 64, 1, 100);
00183 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00184 st->codec->codec_id = CODEC_ID_JACOSUB;
00185
00186 jacosub->timeres = 30;
00187
00188 av_bprint_init(&header, 1024+FF_INPUT_BUFFER_PADDING_SIZE, 4096);
00189
00190 while (!url_feof(pb)) {
00191 int cmd_len;
00192 const char *p = line;
00193 int64_t pos = avio_tell(pb);
00194
00195 ff_get_line(pb, line, sizeof(line));
00196
00197 p = jss_skip_whitespace(p);
00198
00199
00200 if (merge_line || timed_line(p)) {
00201 SubEntry *subs, *sub;
00202 const int len = strlen(line);
00203
00204 if (merge_line) {
00205 char *tmp;
00206 const int old_len = strlen(sub->line);
00207
00208 sub = &subs[jacosub->nsub];
00209 tmp = av_realloc(sub->line, old_len + len + 1);
00210 if (!tmp)
00211 return AVERROR(ENOMEM);
00212 sub->line = tmp;
00213 strcpy(sub->line + old_len, line);
00214 } else {
00215 subs = av_realloc(jacosub->subs,
00216 sizeof(*jacosub->subs) * (jacosub->nsub+1));
00217 if (!subs)
00218 return AVERROR(ENOMEM);
00219 jacosub->subs = subs;
00220 sub = &subs[jacosub->nsub];
00221 sub->pos = pos;
00222 sub->line = av_strdup(line);
00223 if (!sub->line)
00224 return AVERROR(ENOMEM);
00225 }
00226 merge_line = len > 1 && !strcmp(&line[len - 2], "\\\n");
00227 if (!merge_line)
00228 jacosub->nsub++;
00229 continue;
00230 }
00231
00232
00233 if (*p != '#')
00234 continue;
00235 p++;
00236 i = get_jss_cmd(p[0]);
00237 if (i == -1)
00238 continue;
00239
00240
00241 cmd_len = strlen(cmds[i]);
00242 if (av_strncasecmp(p, cmds[i], cmd_len) == 0)
00243 p += cmd_len;
00244 else
00245 p++;
00246 p = jss_skip_whitespace(p);
00247
00248
00249 switch (cmds[i][0]) {
00250 case 'S':
00251 if (!shift_set) {
00252 jacosub->shift = get_shift(jacosub->timeres, p);
00253 shift_set = 1;
00254 }
00255 av_bprintf(&header, "#S %s", p);
00256 break;
00257 case 'T':
00258 jacosub->timeres = strtol(p, NULL, 10);
00259 av_bprintf(&header, "#T %s", p);
00260 break;
00261 }
00262 }
00263
00264
00265 av_bprint_finalize(&header, (char **)&st->codec->extradata);
00266 st->codec->extradata_size = header.len + 1;
00267
00268
00269
00270 for (i = 0; i < jacosub->nsub; i++) {
00271 SubEntry *sub = &jacosub->subs[i];
00272 read_ts(jacosub, sub->line, &sub->start, &sub->end);
00273 }
00274 qsort(jacosub->subs, jacosub->nsub, sizeof(*jacosub->subs), cmp_timed_sub);
00275
00276 return 0;
00277 }
00278
00279 static int jacosub_read_packet(AVFormatContext *s, AVPacket *pkt)
00280 {
00281 int res;
00282 JACOsubContext *jacosub = s->priv_data;
00283 const SubEntry *sub = &jacosub->subs[jacosub->sid++];
00284
00285 if (jacosub->sid == jacosub->nsub)
00286 return AVERROR_EOF;
00287 res = av_new_packet(pkt, strlen(sub->line));
00288 if (res)
00289 return res;
00290 strcpy(pkt->data, sub->line);
00291 pkt->flags |= AV_PKT_FLAG_KEY;
00292 pkt->pos = sub->pos;
00293 pkt->pts = pkt->dts = sub->start;
00294 pkt->duration = sub->end - sub->start;
00295 return 0;
00296 }
00297
00298 AVInputFormat ff_jacosub_demuxer = {
00299 .name = "jacosub",
00300 .long_name = NULL_IF_CONFIG_SMALL("JACOsub subtitle format"),
00301 .priv_data_size = sizeof(JACOsubContext),
00302 .read_probe = jacosub_probe,
00303 .read_header = jacosub_read_header,
00304 .read_packet = jacosub_read_packet,
00305 .read_close = jacosub_read_close,
00306 .flags = AVFMT_GENERIC_INDEX,
00307 };