]> git.sesse.net Git - ffmpeg/blob - libavformat/microdvddec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / microdvddec.c
1 /*
2  * MicroDVD subtitle demuxer
3  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "avformat.h"
23 #include "internal.h"
24 #include "libavutil/intreadwrite.h"
25
26 #define MAX_LINESIZE 2048
27
28
29 typedef struct {
30     uint8_t lines[3][MAX_LINESIZE];
31     int64_t pos[3];
32     AVPacket last_pkt;
33     int last_pkt_ready;
34 } MicroDVDContext;
35
36
37 static int microdvd_probe(AVProbeData *p)
38 {
39     unsigned char c, *ptr = p->buf;
40     int i;
41
42     if (AV_RB24(ptr) == 0xEFBBBF)
43         ptr += 3;  /* skip UTF-8 BOM */
44
45     for (i=0; i<3; i++) {
46         if (sscanf(ptr, "{%*d}{}%c",     &c) != 1 &&
47             sscanf(ptr, "{%*d}{%*d}%c",  &c) != 1 &&
48             sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
49             return 0;
50         ptr += strcspn(ptr, "\n") + 1;
51     }
52     return AVPROBE_SCORE_MAX;
53 }
54
55 static int microdvd_read_header(AVFormatContext *s)
56 {
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);
60     int i, frame;
61     double fps;
62     char c;
63
64     if (!st)
65         return -1;
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);
76             i--;
77         }
78     }
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;
82     return 0;
83 }
84
85 static int64_t get_pts(const char *buf)
86 {
87     int frame;
88     char c;
89
90     if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
91         return frame;
92     return AV_NOPTS_VALUE;
93 }
94
95 static int get_duration(const char *buf)
96 {
97     int frame_start, frame_end;
98
99     if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
100         return frame_end - frame_start;
101     return -1;
102 }
103
104 static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
105 {
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;
110
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;
115         return 0;
116     }
117
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;
124             break;
125         }
126     }
127     if (!len)
128         len = ff_get_line(s->pb, buffer, sizeof(buffer));
129
130     if (microdvd->last_pkt.duration == -1 && !buffer[0]) {
131         // if the previous subtitle line had no duration, last until the end of
132         // the presentation
133         microdvd->last_pkt.duration = 0;
134         *pkt = microdvd->last_pkt;
135         pkt->duration = -1;
136         res = 0;
137     } else if (buffer[0] && !(res = av_new_packet(pkt, len))) {
138         memcpy(pkt->data, buffer, len);
139         pkt->flags |= AV_PKT_FLAG_KEY;
140         pkt->pos = pos;
141         pkt->pts = pkt->dts = get_pts(buffer);
142
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
148                 // and raise it
149                 AVPacket tmp_pkt;
150
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;
155                 *pkt = tmp_pkt;
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
159                 // event.
160                 microdvd->last_pkt = *pkt;
161                 res = AVERROR(EAGAIN);
162             }
163         }
164     }
165     return res;
166 }
167
168 AVInputFormat ff_microdvd_demuxer = {
169     .name           = "microdvd",
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,
176 };