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