00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "internal.h"
00024 #include "libavutil/bprint.h"
00025 #include "libavutil/intreadwrite.h"
00026
00027 static int srt_probe(AVProbeData *p)
00028 {
00029 unsigned char *ptr = p->buf;
00030 int i, v, num = 0;
00031
00032 if (AV_RB24(ptr) == 0xEFBBBF)
00033 ptr += 3;
00034
00035 for (i=0; i<2; i++) {
00036 if ((num == i || num + 1 == i)
00037 && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
00038 return AVPROBE_SCORE_MAX;
00039 num = atoi(ptr);
00040 ptr += strcspn(ptr, "\n") + 1;
00041 }
00042 return 0;
00043 }
00044
00045 static int srt_read_header(AVFormatContext *s)
00046 {
00047 AVStream *st = avformat_new_stream(s, NULL);
00048 if (!st)
00049 return AVERROR(ENOMEM);
00050 avpriv_set_pts_info(st, 64, 1, 1000);
00051 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00052 st->codec->codec_id = AV_CODEC_ID_SUBRIP;
00053 return 0;
00054 }
00055
00056 static int64_t get_pts(const char **buf, int *duration,
00057 int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
00058 {
00059 int i;
00060
00061 for (i=0; i<2; i++) {
00062 int hh1, mm1, ss1, ms1;
00063 int hh2, mm2, ss2, ms2;
00064 if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
00065 "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
00066 &hh1, &mm1, &ss1, &ms1,
00067 &hh2, &mm2, &ss2, &ms2,
00068 x1, x2, y1, y2) >= 8) {
00069 int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
00070 int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
00071 *duration = end - start;
00072 *buf += strcspn(*buf, "\n") + 1;
00073 return start;
00074 }
00075 *buf += strcspn(*buf, "\n") + 1;
00076 }
00077 return AV_NOPTS_VALUE;
00078 }
00079
00080 static inline int is_eol(char c)
00081 {
00082 return c == '\r' || c == '\n';
00083 }
00084
00085 static void read_chunk(AVIOContext *pb, AVBPrint *buf)
00086 {
00087 char eol_buf[5];
00088 int n = 0, i = 0, nb_eol = 0;
00089
00090 for (;;) {
00091 char c = avio_r8(pb);
00092
00093 if (!c)
00094 break;
00095
00096
00097 if (n == 0 && is_eol(c))
00098 continue;
00099
00100
00101 if (is_eol(c)) {
00102 nb_eol += c == '\n';
00103 if (nb_eol == 2)
00104 break;
00105 eol_buf[i++] = c;
00106 if (i == sizeof(eol_buf) - 1)
00107 break;
00108 continue;
00109 }
00110
00111
00112
00113 if (i) {
00114 eol_buf[i] = 0;
00115 av_bprintf(buf, "%s", eol_buf);
00116 i = nb_eol = 0;
00117 }
00118
00119 av_bprint_chars(buf, c, 1);
00120 n++;
00121 }
00122
00123
00124
00125
00126 if (buf->len)
00127 av_bprintf(buf, "\n");
00128 }
00129
00130 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
00131 {
00132 AVBPrint buf;
00133 int64_t pos = avio_tell(s->pb);
00134 int res = AVERROR_EOF;
00135
00136 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
00137 read_chunk(s->pb, &buf);
00138
00139 if (buf.len) {
00140 int64_t pts;
00141 int duration, pkt_size;
00142 const char *ptr = buf.str;
00143 int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
00144
00145 pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
00146 pkt_size = buf.len - (ptr - buf.str);
00147 if (pts != AV_NOPTS_VALUE && !(res = av_new_packet(pkt, pkt_size))) {
00148 memcpy(pkt->data, ptr, pkt->size);
00149 pkt->flags |= AV_PKT_FLAG_KEY;
00150 pkt->pos = pos;
00151 pkt->pts = pkt->dts = pts;
00152 pkt->duration = duration;
00153 if (x1 != -1) {
00154 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, 16);
00155 if (p) {
00156 AV_WL32(p, x1);
00157 AV_WL32(p + 4, y1);
00158 AV_WL32(p + 8, x2);
00159 AV_WL32(p + 12, y2);
00160 }
00161 }
00162 }
00163 }
00164 av_bprint_finalize(&buf, NULL);
00165 return res;
00166 }
00167
00168 AVInputFormat ff_srt_demuxer = {
00169 .name = "srt",
00170 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
00171 .read_probe = srt_probe,
00172 .read_header = srt_read_header,
00173 .read_packet = srt_read_packet,
00174 .flags = AVFMT_GENERIC_INDEX,
00175 };