00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "subtitles.h"
00026 #include "libavutil/intreadwrite.h"
00027 
00028 #define MAX_LINESIZE 2048
00029 
00030 
00031 typedef struct {
00032     FFDemuxSubtitlesQueue q;
00033 } MicroDVDContext;
00034 
00035 
00036 static int microdvd_probe(AVProbeData *p)
00037 {
00038     unsigned char c, *ptr = p->buf;
00039     int i;
00040 
00041     if (AV_RB24(ptr) == 0xEFBBBF)
00042         ptr += 3;  
00043 
00044     for (i=0; i<3; i++) {
00045         if (sscanf(ptr, "{%*d}{}%c",     &c) != 1 &&
00046             sscanf(ptr, "{%*d}{%*d}%c",  &c) != 1 &&
00047             sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
00048             return 0;
00049         ptr += strcspn(ptr, "\n") + 1;
00050     }
00051     return AVPROBE_SCORE_MAX;
00052 }
00053 
00054 static int64_t get_pts(const char *buf)
00055 {
00056     int frame;
00057     char c;
00058 
00059     if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
00060         return frame;
00061     return AV_NOPTS_VALUE;
00062 }
00063 
00064 static int get_duration(const char *buf)
00065 {
00066     int frame_start, frame_end;
00067 
00068     if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
00069         return frame_end - frame_start;
00070     return -1;
00071 }
00072 
00073 static int microdvd_read_header(AVFormatContext *s)
00074 {
00075     AVRational pts_info = (AVRational){ 2997, 125 };  
00076     MicroDVDContext *microdvd = s->priv_data;
00077     AVStream *st = avformat_new_stream(s, NULL);
00078     int i = 0;
00079     char line[MAX_LINESIZE];
00080 
00081     if (!st)
00082         return AVERROR(ENOMEM);
00083 
00084     while (!url_feof(s->pb)) {
00085         AVPacket *sub;
00086         int64_t pos = avio_tell(s->pb);
00087         int len = ff_get_line(s->pb, line, sizeof(line));
00088 
00089         if (!len)
00090             break;
00091         if (i < 3) {
00092             int frame;
00093             double fps;
00094             char c;
00095 
00096             i++;
00097             if ((sscanf(line, "{%d}{}%6lf",    &frame, &fps) == 2 ||
00098                  sscanf(line, "{%d}{%*d}%6lf", &frame, &fps) == 2)
00099                 && frame <= 1 && fps > 3 && fps < 100)
00100                 pts_info = av_d2q(fps, 100000);
00101             if (!st->codec->extradata && sscanf(line, "{DEFAULT}{}%c", &c) == 1) {
00102                 st->codec->extradata = av_strdup(line + 11);
00103                 if (!st->codec->extradata)
00104                     return AVERROR(ENOMEM);
00105                 st->codec->extradata_size = strlen(st->codec->extradata) + 1;
00106                 continue;
00107             }
00108         }
00109         sub = ff_subtitles_queue_insert(µdvd->q, line, len, 0);
00110         if (!sub)
00111             return AVERROR(ENOMEM);
00112         sub->pos = pos;
00113         sub->pts = get_pts(sub->data);
00114         sub->duration = get_duration(sub->data);
00115     }
00116     ff_subtitles_queue_finalize(µdvd->q);
00117     avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
00118     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00119     st->codec->codec_id   = AV_CODEC_ID_MICRODVD;
00120     return 0;
00121 }
00122 
00123 static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
00124 {
00125     MicroDVDContext *microdvd = s->priv_data;
00126     return ff_subtitles_queue_read_packet(µdvd->q, pkt);
00127 }
00128 
00129 static int microdvd_read_close(AVFormatContext *s)
00130 {
00131     MicroDVDContext *microdvd = s->priv_data;
00132     ff_subtitles_queue_clean(µdvd->q);
00133     return 0;
00134 }
00135 
00136 AVInputFormat ff_microdvd_demuxer = {
00137     .name           = "microdvd",
00138     .long_name      = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
00139     .priv_data_size = sizeof(MicroDVDContext),
00140     .read_probe     = microdvd_probe,
00141     .read_header    = microdvd_read_header,
00142     .read_packet    = microdvd_read_packet,
00143     .read_close     = microdvd_read_close,
00144     .flags          = AVFMT_GENERIC_INDEX,
00145 };