yading@11
|
1 /*
|
yading@11
|
2 * Copyright (c) 2012 Clément Bœsch
|
yading@11
|
3 *
|
yading@11
|
4 * This file is part of FFmpeg.
|
yading@11
|
5 *
|
yading@11
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
8 * License as published by the Free Software Foundation; either
|
yading@11
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
10 *
|
yading@11
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
14 * Lesser General Public License for more details.
|
yading@11
|
15 *
|
yading@11
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
19 */
|
yading@11
|
20
|
yading@11
|
21 /**
|
yading@11
|
22 * @file
|
yading@11
|
23 * MPlayer subtitles format demuxer
|
yading@11
|
24 */
|
yading@11
|
25
|
yading@11
|
26 #include "avformat.h"
|
yading@11
|
27 #include "internal.h"
|
yading@11
|
28 #include "subtitles.h"
|
yading@11
|
29
|
yading@11
|
30 typedef struct {
|
yading@11
|
31 FFDemuxSubtitlesQueue q;
|
yading@11
|
32 } MPSubContext;
|
yading@11
|
33
|
yading@11
|
34 static int mpsub_probe(AVProbeData *p)
|
yading@11
|
35 {
|
yading@11
|
36 const char *ptr = p->buf;
|
yading@11
|
37 const char *ptr_end = p->buf + p->buf_size;
|
yading@11
|
38
|
yading@11
|
39 while (ptr < ptr_end) {
|
yading@11
|
40 int n;
|
yading@11
|
41
|
yading@11
|
42 if (!memcmp(ptr, "FORMAT=TIME", 11) ||
|
yading@11
|
43 sscanf(ptr, "FORMAT=%d", &n) == 1)
|
yading@11
|
44 return AVPROBE_SCORE_MAX/2;
|
yading@11
|
45 ptr += strcspn(ptr, "\n") + 1;
|
yading@11
|
46 }
|
yading@11
|
47 return 0;
|
yading@11
|
48 }
|
yading@11
|
49
|
yading@11
|
50 static int mpsub_read_header(AVFormatContext *s)
|
yading@11
|
51 {
|
yading@11
|
52 MPSubContext *mpsub = s->priv_data;
|
yading@11
|
53 AVStream *st;
|
yading@11
|
54 AVBPrint buf;
|
yading@11
|
55 AVRational pts_info = (AVRational){ 100, 1 }; // ts based by default
|
yading@11
|
56 int res = 0;
|
yading@11
|
57 float multiplier = 100.0;
|
yading@11
|
58 float current_pts = 0;
|
yading@11
|
59
|
yading@11
|
60 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
|
yading@11
|
61
|
yading@11
|
62 while (!url_feof(s->pb)) {
|
yading@11
|
63 char line[1024];
|
yading@11
|
64 float start, duration;
|
yading@11
|
65 int fps, len = ff_get_line(s->pb, line, sizeof(line));
|
yading@11
|
66
|
yading@11
|
67 if (!len)
|
yading@11
|
68 break;
|
yading@11
|
69
|
yading@11
|
70 line[strcspn(line, "\r\n")] = 0;
|
yading@11
|
71
|
yading@11
|
72 if (sscanf(line, "FORMAT=%d", &fps) == 1 && fps > 3 && fps < 100) {
|
yading@11
|
73 /* frame based timing */
|
yading@11
|
74 pts_info = (AVRational){ fps, 1 };
|
yading@11
|
75 multiplier = 1.0;
|
yading@11
|
76 } else if (sscanf(line, "%f %f", &start, &duration) == 2) {
|
yading@11
|
77 AVPacket *sub;
|
yading@11
|
78 const int64_t pos = avio_tell(s->pb);
|
yading@11
|
79
|
yading@11
|
80 ff_subtitles_read_chunk(s->pb, &buf);
|
yading@11
|
81 if (buf.len) {
|
yading@11
|
82 sub = ff_subtitles_queue_insert(&mpsub->q, buf.str, buf.len, 0);
|
yading@11
|
83 if (!sub) {
|
yading@11
|
84 res = AVERROR(ENOMEM);
|
yading@11
|
85 goto end;
|
yading@11
|
86 }
|
yading@11
|
87 sub->pts = (int64_t)(current_pts + start*multiplier);
|
yading@11
|
88 sub->duration = (int)(duration * multiplier);
|
yading@11
|
89 current_pts += (start + duration) * multiplier;
|
yading@11
|
90 sub->pos = pos;
|
yading@11
|
91 }
|
yading@11
|
92 }
|
yading@11
|
93 }
|
yading@11
|
94
|
yading@11
|
95 st = avformat_new_stream(s, NULL);
|
yading@11
|
96 if (!st)
|
yading@11
|
97 return AVERROR(ENOMEM);
|
yading@11
|
98 avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
|
yading@11
|
99 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
|
yading@11
|
100 st->codec->codec_id = AV_CODEC_ID_TEXT;
|
yading@11
|
101
|
yading@11
|
102 ff_subtitles_queue_finalize(&mpsub->q);
|
yading@11
|
103
|
yading@11
|
104 end:
|
yading@11
|
105 av_bprint_finalize(&buf, NULL);
|
yading@11
|
106 return res;
|
yading@11
|
107 }
|
yading@11
|
108
|
yading@11
|
109 static int mpsub_read_packet(AVFormatContext *s, AVPacket *pkt)
|
yading@11
|
110 {
|
yading@11
|
111 MPSubContext *mpsub = s->priv_data;
|
yading@11
|
112 return ff_subtitles_queue_read_packet(&mpsub->q, pkt);
|
yading@11
|
113 }
|
yading@11
|
114
|
yading@11
|
115 static int mpsub_read_seek(AVFormatContext *s, int stream_index,
|
yading@11
|
116 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
|
yading@11
|
117 {
|
yading@11
|
118 MPSubContext *mpsub = s->priv_data;
|
yading@11
|
119 return ff_subtitles_queue_seek(&mpsub->q, s, stream_index,
|
yading@11
|
120 min_ts, ts, max_ts, flags);
|
yading@11
|
121 }
|
yading@11
|
122
|
yading@11
|
123 static int mpsub_read_close(AVFormatContext *s)
|
yading@11
|
124 {
|
yading@11
|
125 MPSubContext *mpsub = s->priv_data;
|
yading@11
|
126 ff_subtitles_queue_clean(&mpsub->q);
|
yading@11
|
127 return 0;
|
yading@11
|
128 }
|
yading@11
|
129
|
yading@11
|
130 AVInputFormat ff_mpsub_demuxer = {
|
yading@11
|
131 .name = "mpsub",
|
yading@11
|
132 .long_name = NULL_IF_CONFIG_SMALL("MPlayer subtitles"),
|
yading@11
|
133 .priv_data_size = sizeof(MPSubContext),
|
yading@11
|
134 .read_probe = mpsub_probe,
|
yading@11
|
135 .read_header = mpsub_read_header,
|
yading@11
|
136 .read_packet = mpsub_read_packet,
|
yading@11
|
137 .read_seek2 = mpsub_read_seek,
|
yading@11
|
138 .read_close = mpsub_read_close,
|
yading@11
|
139 .extensions = "sub",
|
yading@11
|
140 };
|