]> git.sesse.net Git - ffmpeg/blob - libavformat/srtdec.c
srt: make the demuxer output SubRip packets.
[ffmpeg] / libavformat / srtdec.c
1 /*
2  * SubRip 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 static int srt_probe(AVProbeData *p)
27 {
28     unsigned char *ptr = p->buf;
29     int i, v, num = 0;
30
31     if (AV_RB24(ptr) == 0xEFBBBF)
32         ptr += 3;  /* skip UTF-8 BOM */
33
34     for (i=0; i<2; i++) {
35         if (num == i && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
36             return AVPROBE_SCORE_MAX;
37         num = atoi(ptr);
38         ptr += strcspn(ptr, "\n") + 1;
39     }
40     return 0;
41 }
42
43 static int srt_read_header(AVFormatContext *s)
44 {
45     AVStream *st = avformat_new_stream(s, NULL);
46     if (!st)
47         return AVERROR(ENOMEM);
48     avpriv_set_pts_info(st, 64, 1, 1000);
49     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
50     st->codec->codec_id   = AV_CODEC_ID_SUBRIP;
51     return 0;
52 }
53
54 static int64_t get_pts(char **buf, int *duration,
55                        int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
56 {
57     int i;
58
59     for (i=0; i<2; i++) {
60         int hh1, mm1, ss1, ms1;
61         int hh2, mm2, ss2, ms2;
62         if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
63                    "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
64                    &hh1, &mm1, &ss1, &ms1,
65                    &hh2, &mm2, &ss2, &ms2,
66                    x1, x2, y1, y2) >= 8) {
67             int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
68             int64_t end   = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
69             *duration = end - start;
70             *buf += strcspn(*buf, "\n") + 1;
71             return start;
72         }
73         *buf += strcspn(*buf, "\n") + 1;
74     }
75     return AV_NOPTS_VALUE;
76 }
77
78 static inline int is_eol(char c)
79 {
80     return c == '\r' || c == '\n';
81 }
82
83 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
84 {
85     char buffer[2048], *ptr = buffer, *ptr2;
86     int64_t pos = avio_tell(s->pb);
87     int res = AVERROR_EOF;
88
89     do {
90         ptr2 = ptr;
91         ptr += ff_get_line(s->pb, ptr, sizeof(buffer)+buffer-ptr);
92     } while (!is_eol(*ptr2) && !url_feof(s->pb) && ptr-buffer<sizeof(buffer)-1);
93
94     if (buffer[0]) {
95         int64_t pts;
96         int duration;
97         const char *end = ptr;
98         int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
99
100         ptr = buffer;
101         pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
102         if (pts != AV_NOPTS_VALUE &&
103             !(res = av_new_packet(pkt, end - ptr))) {
104             memcpy(pkt->data, ptr, pkt->size);
105             pkt->flags |= AV_PKT_FLAG_KEY;
106             pkt->pos = pos;
107             pkt->pts = pkt->dts = pts;
108             pkt->duration = duration;
109             if (x1 != -1) {
110                 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, 16);
111                 if (p) {
112                     AV_WL32(p,      x1);
113                     AV_WL32(p +  4, y1);
114                     AV_WL32(p +  8, x2);
115                     AV_WL32(p + 12, y2);
116                 }
117             }
118         }
119     }
120     return res;
121 }
122
123 AVInputFormat ff_srt_demuxer = {
124     .name        = "srt",
125     .long_name   = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
126     .read_probe  = srt_probe,
127     .read_header = srt_read_header,
128     .read_packet = srt_read_packet,
129     .flags       = AVFMT_GENERIC_INDEX,
130 };