]> 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 } MicroDVDContext;
33
34
35 static int microdvd_probe(AVProbeData *p)
36 {
37     unsigned char c, *ptr = p->buf;
38     int i;
39
40     if (AV_RB24(ptr) == 0xEFBBBF)
41         ptr += 3;  /* skip UTF-8 BOM */
42
43     for (i=0; i<3; i++) {
44         if (sscanf(ptr, "{%*d}{}%c",     &c) != 1 &&
45             sscanf(ptr, "{%*d}{%*d}%c",  &c) != 1 &&
46             sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
47             return 0;
48         ptr += strcspn(ptr, "\n") + 1;
49     }
50     return AVPROBE_SCORE_MAX;
51 }
52
53 static int microdvd_read_header(AVFormatContext *s)
54 {
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);
58     int i, frame;
59     double fps;
60     char c;
61
62     if (!st)
63         return -1;
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);
74             i--;
75         }
76     }
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;
80     return 0;
81 }
82
83 static int64_t get_pts(const char *buf)
84 {
85     int frame;
86     char c;
87
88     if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
89         return frame;
90     return AV_NOPTS_VALUE;
91 }
92
93 static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
94 {
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;
99
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;
106             break;
107         }
108     }
109     if (!len)
110         len = ff_get_line(s->pb, buffer, sizeof(buffer));
111
112     if (buffer[0] && !(res = av_new_packet(pkt, len))) {
113         memcpy(pkt->data, buffer, len);
114         pkt->flags |= AV_PKT_FLAG_KEY;
115         pkt->pos = pos;
116         pkt->pts = pkt->dts = get_pts(buffer);
117     }
118     return res;
119 }
120
121 AVInputFormat ff_microdvd_demuxer = {
122     .name           = "microdvd",
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,
129 };