annotate srtdec.c @ 6394:896acb967d32 libavformat

add FF_API_REGISTER_PROTOCOL define to disable the deprecated register_protocol() function
author aurel
date Thu, 19 Aug 2010 21:21:32 +0000
parents 2b1ab404f1cf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6308
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
1 /*
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
2 * SubRip subtitle demuxer
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
4 *
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
5 * This file is part of FFmpeg.
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
6 *
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
11 *
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
15 * Lesser General Public License for more details.
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
16 *
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
20 */
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
21
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
22 #include "avformat.h"
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
23 #include "internal.h"
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
24 #include "libavutil/intreadwrite.h"
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
25
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
26 static int srt_probe(AVProbeData *p)
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
27 {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
28 unsigned char *ptr = p->buf;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
29 int i, v, num = 0;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
30
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
31 if (AV_RB24(ptr) == 0xEFBBBF)
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
32 ptr += 3; /* skip UTF-8 BOM */
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
33
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
34 for (i=0; i<2; i++) {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
35 if (num == i && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
36 return AVPROBE_SCORE_MAX;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
37 num = atoi(ptr);
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
38 ptr += strcspn(ptr, "\n") + 1;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
39 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
40 return 0;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
41 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
42
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
43 static int srt_read_header(AVFormatContext *s, AVFormatParameters *ap)
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
44 {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
45 AVStream *st = av_new_stream(s, 0);
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
46 if (!st)
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
47 return -1;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
48 av_set_pts_info(st, 64, 1, 1000);
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
49 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
50 st->codec->codec_id = CODEC_ID_SRT;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
51 return 0;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
52 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
53
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
54 static int64_t get_pts(const char *buf)
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
55 {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
56 int i, v, hour, min, sec, hsec;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
57
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
58 for (i=0; i<2; i++) {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
59 if (sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %*d:%*2d:%*2d%*1[,.]%3d",
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
60 &hour, &min, &sec, &hsec, &v) == 5) {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
61 min += 60*hour;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
62 sec += 60*min;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
63 return sec*1000+hsec;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
64 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
65 buf += strcspn(buf, "\n") + 1;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
66 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
67 return AV_NOPTS_VALUE;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
68 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
69
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
70 static inline int is_eol(char c)
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
71 {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
72 return c == '\r' || c == '\n';
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
73 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
74
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
75 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
76 {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
77 char buffer[2048], *ptr = buffer, *ptr2;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
78 int64_t pos = url_ftell(s->pb);
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
79 int res = AVERROR_EOF;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
80
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
81 do {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
82 ptr2 = ptr;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
83 ptr += ff_get_line(s->pb, ptr, sizeof(buffer)+buffer-ptr);
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
84 } while (!is_eol(*ptr2) && !url_feof(s->pb) && ptr-buffer<sizeof(buffer)-1);
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
85
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
86 if (buffer[0] && !(res = av_new_packet(pkt, ptr-buffer))) {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
87 memcpy(pkt->data, buffer, pkt->size);
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
88 pkt->flags |= AV_PKT_FLAG_KEY;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
89 pkt->pos = pos;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
90 pkt->pts = pkt->dts = get_pts(pkt->data);
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
91 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
92 return res;
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
93 }
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
94
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
95 AVInputFormat srt_demuxer = {
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
96 .name = "srt",
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
97 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle format"),
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
98 .read_probe = srt_probe,
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
99 .read_header = srt_read_header,
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
100 .read_packet = srt_read_packet,
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
101 .flags = AVFMT_GENERIC_INDEX,
2b1ab404f1cf add SubRip muxer and demuxer
aurel
parents:
diff changeset
102 };