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/intreadwrite.h"
00025
00026 #define MAX_LINESIZE 2048
00027
00028
00029 typedef struct {
00030 uint8_t lines[3][MAX_LINESIZE];
00031 int64_t pos[3];
00032 } MicroDVDContext;
00033
00034
00035 static int microdvd_probe(AVProbeData *p)
00036 {
00037 unsigned char c, *ptr = p->buf;
00038 int i;
00039
00040 if (AV_RB24(ptr) == 0xEFBBBF)
00041 ptr += 3;
00042
00043 for (i=0; i<3; i++) {
00044 if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
00045 sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
00046 sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
00047 return 0;
00048 ptr += strcspn(ptr, "\n") + 1;
00049 }
00050 return AVPROBE_SCORE_MAX;
00051 }
00052
00053 static int microdvd_read_header(AVFormatContext *s)
00054 {
00055 AVRational pts_info = (AVRational){ 2997, 125 };
00056 MicroDVDContext *microdvd = s->priv_data;
00057 AVStream *st = avformat_new_stream(s, NULL);
00058 int i, frame;
00059 double fps;
00060 char c;
00061
00062 if (!st)
00063 return -1;
00064 for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
00065 microdvd->pos[i] = avio_tell(s->pb);
00066 ff_get_line(s->pb, microdvd->lines[i], sizeof(microdvd->lines[i]));
00067 if ((sscanf(microdvd->lines[i], "{%d}{}%6lf", &frame, &fps) == 2 ||
00068 sscanf(microdvd->lines[i], "{%d}{%*d}%6lf", &frame, &fps) == 2)
00069 && frame <= 1 && fps > 3 && fps < 100)
00070 pts_info = av_d2q(fps, 100000);
00071 if (sscanf(microdvd->lines[i], "{DEFAULT}{}%c", &c) == 1) {
00072 st->codec->extradata = av_strdup(microdvd->lines[i] + 11);
00073 st->codec->extradata_size = strlen(st->codec->extradata);
00074 i--;
00075 }
00076 }
00077 avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
00078 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00079 st->codec->codec_id = CODEC_ID_MICRODVD;
00080 return 0;
00081 }
00082
00083 static int64_t get_pts(const char *buf)
00084 {
00085 int frame;
00086 char c;
00087
00088 if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
00089 return frame;
00090 return AV_NOPTS_VALUE;
00091 }
00092
00093 static int get_duration(const char *buf)
00094 {
00095 int frame_start, frame_end;
00096
00097 if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
00098 return frame_end - frame_start;
00099 return 0;
00100 }
00101
00102 static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
00103 {
00104 MicroDVDContext *microdvd = s->priv_data;
00105 char buffer[MAX_LINESIZE];
00106 int64_t pos = avio_tell(s->pb);
00107 int i, len = 0, res = AVERROR_EOF;
00108
00109 for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
00110 if (microdvd->lines[i][0]) {
00111 strcpy(buffer, microdvd->lines[i]);
00112 pos = microdvd->pos[i];
00113 len = strlen(buffer);
00114 microdvd->lines[i][0] = 0;
00115 break;
00116 }
00117 }
00118 if (!len)
00119 len = ff_get_line(s->pb, buffer, sizeof(buffer));
00120
00121 if (buffer[0] && !(res = av_new_packet(pkt, len))) {
00122 memcpy(pkt->data, buffer, len);
00123 pkt->flags |= AV_PKT_FLAG_KEY;
00124 pkt->pos = pos;
00125 pkt->pts = pkt->dts = get_pts(buffer);
00126 if (pkt->pts != AV_NOPTS_VALUE)
00127 pkt->duration = get_duration(buffer);
00128 }
00129 return res;
00130 }
00131
00132 AVInputFormat ff_microdvd_demuxer = {
00133 .name = "microdvd",
00134 .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
00135 .priv_data_size = sizeof(MicroDVDContext),
00136 .read_probe = microdvd_probe,
00137 .read_header = microdvd_read_header,
00138 .read_packet = microdvd_read_packet,
00139 .flags = AVFMT_GENERIC_INDEX,
00140 };