2 * MicroDVD subtitle demuxer
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/intreadwrite.h"
26 #define MAX_LINESIZE 2048
30 uint8_t lines[3][MAX_LINESIZE];
35 static int microdvd_probe(AVProbeData *p)
37 unsigned char c, *ptr = p->buf;
40 if (AV_RB24(ptr) == 0xEFBBBF)
41 ptr += 3; /* skip UTF-8 BOM */
44 if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
45 sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
46 sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
48 ptr += strcspn(ptr, "\n") + 1;
50 return AVPROBE_SCORE_MAX;
53 static int microdvd_read_header(AVFormatContext *s)
55 AVRational pts_info = (AVRational){ 2997, 125 }; /* default: 23.976 fps */
56 MicroDVDContext *microdvd = s->priv_data;
57 AVStream *st = avformat_new_stream(s, NULL);
64 for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
65 microdvd->pos[i] = avio_tell(s->pb);
66 ff_get_line(s->pb, microdvd->lines[i], sizeof(microdvd->lines[i]));
67 if ((sscanf(microdvd->lines[i], "{%d}{}%6lf", &frame, &fps) == 2 ||
68 sscanf(microdvd->lines[i], "{%d}{%*d}%6lf", &frame, &fps) == 2)
69 && frame <= 1 && fps > 3 && fps < 100)
70 pts_info = av_d2q(fps, 100000);
71 if (sscanf(microdvd->lines[i], "{DEFAULT}{}%c", &c) == 1) {
72 st->codec->extradata = av_strdup(microdvd->lines[i] + 11);
73 st->codec->extradata_size = strlen(st->codec->extradata);
77 avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
78 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
79 st->codec->codec_id = CODEC_ID_MICRODVD;
83 static int64_t get_pts(const char *buf)
88 if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
90 return AV_NOPTS_VALUE;
93 static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
95 MicroDVDContext *microdvd = s->priv_data;
96 char buffer[MAX_LINESIZE];
97 int64_t pos = avio_tell(s->pb);
98 int i, len = 0, res = AVERROR_EOF;
100 for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
101 if (microdvd->lines[i][0]) {
102 strcpy(buffer, microdvd->lines[i]);
103 pos = microdvd->pos[i];
104 len = strlen(buffer);
105 microdvd->lines[i][0] = 0;
110 len = ff_get_line(s->pb, buffer, sizeof(buffer));
112 if (buffer[0] && !(res = av_new_packet(pkt, len))) {
113 memcpy(pkt->data, buffer, len);
114 pkt->flags |= AV_PKT_FLAG_KEY;
116 pkt->pts = pkt->dts = get_pts(buffer);
121 AVInputFormat ff_microdvd_demuxer = {
123 .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
124 .priv_data_size = sizeof(MicroDVDContext),
125 .read_probe = microdvd_probe,
126 .read_header = microdvd_read_header,
127 .read_packet = microdvd_read_packet,
128 .flags = AVFMT_GENERIC_INDEX,