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];
37 static int microdvd_probe(AVProbeData *p)
39 unsigned char c, *ptr = p->buf;
42 if (AV_RB24(ptr) == 0xEFBBBF)
43 ptr += 3; /* skip UTF-8 BOM */
46 if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
47 sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
48 sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
50 ptr += strcspn(ptr, "\n") + 1;
52 return AVPROBE_SCORE_MAX;
55 static int microdvd_read_header(AVFormatContext *s)
57 AVRational pts_info = (AVRational){ 2997, 125 }; /* default: 23.976 fps */
58 MicroDVDContext *microdvd = s->priv_data;
59 AVStream *st = avformat_new_stream(s, NULL);
66 for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
67 microdvd->pos[i] = avio_tell(s->pb);
68 ff_get_line(s->pb, microdvd->lines[i], sizeof(microdvd->lines[i]));
69 if ((sscanf(microdvd->lines[i], "{%d}{}%6lf", &frame, &fps) == 2 ||
70 sscanf(microdvd->lines[i], "{%d}{%*d}%6lf", &frame, &fps) == 2)
71 && frame <= 1 && fps > 3 && fps < 100)
72 pts_info = av_d2q(fps, 100000);
73 if (sscanf(microdvd->lines[i], "{DEFAULT}{}%c", &c) == 1) {
74 st->codec->extradata = av_strdup(microdvd->lines[i] + 11);
75 st->codec->extradata_size = strlen(st->codec->extradata);
79 avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
80 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
81 st->codec->codec_id = CODEC_ID_MICRODVD;
85 static int64_t get_pts(const char *buf)
90 if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
92 return AV_NOPTS_VALUE;
95 static int get_duration(const char *buf)
97 int frame_start, frame_end;
99 if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
100 return frame_end - frame_start;
104 static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
106 MicroDVDContext *microdvd = s->priv_data;
107 char buffer[MAX_LINESIZE];
108 int64_t pos = avio_tell(s->pb);
109 int i, len = 0, res = AVERROR_EOF;
111 // last packet has its duration set but couldn't be raised earlier
112 if (microdvd->last_pkt_ready) {
113 *pkt = microdvd->last_pkt;
114 microdvd->last_pkt_ready = 0;
118 for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
119 if (microdvd->lines[i][0]) {
120 strcpy(buffer, microdvd->lines[i]);
121 pos = microdvd->pos[i];
122 len = strlen(buffer);
123 microdvd->lines[i][0] = 0;
128 len = ff_get_line(s->pb, buffer, sizeof(buffer));
130 if (microdvd->last_pkt.duration == -1 && !buffer[0]) {
131 // if the previous subtitle line had no duration, last until the end of
133 microdvd->last_pkt.duration = 0;
134 *pkt = microdvd->last_pkt;
137 } else if (buffer[0] && !(res = av_new_packet(pkt, len))) {
138 memcpy(pkt->data, buffer, len);
139 pkt->flags |= AV_PKT_FLAG_KEY;
141 pkt->pts = pkt->dts = get_pts(buffer);
143 if (pkt->pts != AV_NOPTS_VALUE) {
144 pkt->duration = get_duration(buffer);
145 if (microdvd->last_pkt.duration == -1) {
146 // previous packet wasn't raised because it was lacking the
147 // duration info, so set its duration with the new packet pts
151 tmp_pkt = microdvd->last_pkt;
152 tmp_pkt.duration = pkt->pts - tmp_pkt.pts;
153 microdvd->last_pkt = *pkt;
154 microdvd->last_pkt_ready = pkt->duration != -1;
156 } else if (pkt->duration == -1) {
157 // no packet without duration queued, and current one is
158 // lacking the duration info, we need to parse another subtitle
160 microdvd->last_pkt = *pkt;
161 res = AVERROR(EAGAIN);
168 AVInputFormat ff_microdvd_demuxer = {
170 .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
171 .priv_data_size = sizeof(MicroDVDContext),
172 .read_probe = microdvd_probe,
173 .read_header = microdvd_read_header,
174 .read_packet = microdvd_read_packet,
175 .flags = AVFMT_GENERIC_INDEX,