00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00027 #include "avformat.h"
00028 #include "internal.h"
00029 #include "subtitles.h"
00030 #include "libavutil/avstring.h"
00031 #include "libavutil/bprint.h"
00032 #include "libavutil/intreadwrite.h"
00033 
00034 typedef struct {
00035     FFDemuxSubtitlesQueue q;
00036 } RealTextContext;
00037 
00038 static int realtext_probe(AVProbeData *p)
00039 {
00040     const unsigned char *ptr = p->buf;
00041 
00042     if (AV_RB24(ptr) == 0xEFBBBF)
00043         ptr += 3;  
00044     return !av_strncasecmp(ptr, "<window", 7) ? AVPROBE_SCORE_MAX/2 : 0;
00045 }
00046 
00047 static int read_ts(const char *s)
00048 {
00049     int hh, mm, ss, ms;
00050 
00051     if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600 + mm*60 + ss) * 100 + ms;
00052     if (sscanf(s, "%u:%u:%u"   , &hh, &mm, &ss     ) == 3) return (hh*3600 + mm*60 + ss) * 100;
00053     if (sscanf(s,    "%u:%u.%u",      &mm, &ss, &ms) == 3) return (          mm*60 + ss) * 100 + ms;
00054     if (sscanf(s,    "%u:%u"   ,      &mm, &ss     ) == 2) return (          mm*60 + ss) * 100;
00055     if (sscanf(s,       "%u.%u",           &ss, &ms) == 2) return (                  ss) * 100 + ms;
00056     return strtol(s, NULL, 10) * 100;
00057 }
00058 
00059 static int realtext_read_header(AVFormatContext *s)
00060 {
00061     RealTextContext *rt = s->priv_data;
00062     AVStream *st = avformat_new_stream(s, NULL);
00063     AVBPrint buf;
00064     char c = 0;
00065     int res = 0, duration = read_ts("60"); 
00066 
00067     if (!st)
00068         return AVERROR(ENOMEM);
00069     avpriv_set_pts_info(st, 64, 1, 100);
00070     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00071     st->codec->codec_id   = AV_CODEC_ID_REALTEXT;
00072 
00073     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
00074 
00075     while (!url_feof(s->pb)) {
00076         AVPacket *sub;
00077         const int64_t pos = avio_tell(s->pb) - (c != 0);
00078         int n = ff_smil_extract_next_chunk(s->pb, &buf, &c);
00079 
00080         if (n == 0)
00081             break;
00082 
00083         if (!av_strncasecmp(buf.str, "<window", 7)) {
00084             
00085             const char *p = ff_smil_get_attr_ptr(buf.str, "duration");
00086 
00087             if (p)
00088                 duration = read_ts(p);
00089             st->codec->extradata = av_strdup(buf.str);
00090             if (!st->codec->extradata) {
00091                 res = AVERROR(ENOMEM);
00092                 goto end;
00093             }
00094             st->codec->extradata_size = buf.len + 1;
00095         } else {
00096             
00097 
00098             int merge = !av_strncasecmp(buf.str, "<time", 5) ? 0 : 1;
00099             sub = ff_subtitles_queue_insert(&rt->q, buf.str, buf.len, merge);
00100             if (!sub) {
00101                 res = AVERROR(ENOMEM);
00102                 goto end;
00103             }
00104             if (!merge) {
00105                 const char *begin = ff_smil_get_attr_ptr(buf.str, "begin");
00106                 const char *end   = ff_smil_get_attr_ptr(buf.str, "end");
00107 
00108                 sub->pos      = pos;
00109                 sub->pts      = begin ? read_ts(begin) : 0;
00110                 sub->duration = end ? (read_ts(end) - sub->pts) : duration;
00111             }
00112         }
00113         av_bprint_clear(&buf);
00114     }
00115     ff_subtitles_queue_finalize(&rt->q);
00116 
00117 end:
00118     av_bprint_finalize(&buf, NULL);
00119     return res;
00120 }
00121 
00122 static int realtext_read_packet(AVFormatContext *s, AVPacket *pkt)
00123 {
00124     RealTextContext *rt = s->priv_data;
00125     return ff_subtitles_queue_read_packet(&rt->q, pkt);
00126 }
00127 
00128 static int realtext_read_close(AVFormatContext *s)
00129 {
00130     RealTextContext *rt = s->priv_data;
00131     ff_subtitles_queue_clean(&rt->q);
00132     return 0;
00133 }
00134 
00135 AVInputFormat ff_realtext_demuxer = {
00136     .name           = "realtext",
00137     .long_name      = NULL_IF_CONFIG_SMALL("RealText subtitle format"),
00138     .priv_data_size = sizeof(RealTextContext),
00139     .read_probe     = realtext_probe,
00140     .read_header    = realtext_read_header,
00141     .read_packet    = realtext_read_packet,
00142     .read_close     = realtext_read_close,
00143     .flags          = AVFMT_GENERIC_INDEX,
00144     .extensions     = "rt",
00145 };