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 * AQTitle subtitles format demuxer
|
yading@11
|
24 *
|
yading@11
|
25 * @see http://web.archive.org/web/20070210095721/http://www.volny.cz/aberka/czech/aqt.html
|
yading@11
|
26 * @see https://trac.annodex.net/wiki/AQTitle
|
yading@11
|
27 */
|
yading@11
|
28
|
yading@11
|
29 #include "avformat.h"
|
yading@11
|
30 #include "internal.h"
|
yading@11
|
31 #include "subtitles.h"
|
yading@11
|
32 #include "libavutil/opt.h"
|
yading@11
|
33
|
yading@11
|
34 typedef struct {
|
yading@11
|
35 const AVClass *class;
|
yading@11
|
36 FFDemuxSubtitlesQueue q;
|
yading@11
|
37 AVRational frame_rate;
|
yading@11
|
38 } AQTitleContext;
|
yading@11
|
39
|
yading@11
|
40 static int aqt_probe(AVProbeData *p)
|
yading@11
|
41 {
|
yading@11
|
42 int frame;
|
yading@11
|
43 const char *ptr = p->buf;
|
yading@11
|
44
|
yading@11
|
45 if (sscanf(ptr, "-->> %d", &frame) == 1)
|
yading@11
|
46 return AVPROBE_SCORE_MAX / 2;
|
yading@11
|
47 return 0;
|
yading@11
|
48 }
|
yading@11
|
49
|
yading@11
|
50 static int aqt_read_header(AVFormatContext *s)
|
yading@11
|
51 {
|
yading@11
|
52 AQTitleContext *aqt = s->priv_data;
|
yading@11
|
53 AVStream *st = avformat_new_stream(s, NULL);
|
yading@11
|
54 int new_event = 1;
|
yading@11
|
55 int64_t pos = 0, frame = AV_NOPTS_VALUE;
|
yading@11
|
56 AVPacket *sub = NULL;
|
yading@11
|
57
|
yading@11
|
58 if (!st)
|
yading@11
|
59 return AVERROR(ENOMEM);
|
yading@11
|
60 avpriv_set_pts_info(st, 64, aqt->frame_rate.den, aqt->frame_rate.num);
|
yading@11
|
61 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
|
yading@11
|
62 st->codec->codec_id = AV_CODEC_ID_TEXT;
|
yading@11
|
63
|
yading@11
|
64 while (!url_feof(s->pb)) {
|
yading@11
|
65 char line[4096];
|
yading@11
|
66 int len = ff_get_line(s->pb, line, sizeof(line));
|
yading@11
|
67
|
yading@11
|
68 if (!len)
|
yading@11
|
69 break;
|
yading@11
|
70
|
yading@11
|
71 line[strcspn(line, "\r\n")] = 0;
|
yading@11
|
72
|
yading@11
|
73 if (sscanf(line, "-->> %"SCNd64, &frame) == 1) {
|
yading@11
|
74 new_event = 1;
|
yading@11
|
75 pos = avio_tell(s->pb);
|
yading@11
|
76 if (sub) {
|
yading@11
|
77 sub->duration = frame - sub->pts;
|
yading@11
|
78 sub = NULL;
|
yading@11
|
79 }
|
yading@11
|
80 } else if (*line) {
|
yading@11
|
81 if (!new_event) {
|
yading@11
|
82 sub = ff_subtitles_queue_insert(&aqt->q, "\n", 1, 1);
|
yading@11
|
83 if (!sub)
|
yading@11
|
84 return AVERROR(ENOMEM);
|
yading@11
|
85 }
|
yading@11
|
86 sub = ff_subtitles_queue_insert(&aqt->q, line, strlen(line), !new_event);
|
yading@11
|
87 if (!sub)
|
yading@11
|
88 return AVERROR(ENOMEM);
|
yading@11
|
89 if (new_event) {
|
yading@11
|
90 sub->pts = frame;
|
yading@11
|
91 sub->duration = -1;
|
yading@11
|
92 sub->pos = pos;
|
yading@11
|
93 }
|
yading@11
|
94 new_event = 0;
|
yading@11
|
95 }
|
yading@11
|
96 }
|
yading@11
|
97
|
yading@11
|
98 ff_subtitles_queue_finalize(&aqt->q);
|
yading@11
|
99 return 0;
|
yading@11
|
100 }
|
yading@11
|
101
|
yading@11
|
102 static int aqt_read_packet(AVFormatContext *s, AVPacket *pkt)
|
yading@11
|
103 {
|
yading@11
|
104 AQTitleContext *aqt = s->priv_data;
|
yading@11
|
105 return ff_subtitles_queue_read_packet(&aqt->q, pkt);
|
yading@11
|
106 }
|
yading@11
|
107
|
yading@11
|
108 static int aqt_read_seek(AVFormatContext *s, int stream_index,
|
yading@11
|
109 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
|
yading@11
|
110 {
|
yading@11
|
111 AQTitleContext *aqt = s->priv_data;
|
yading@11
|
112 return ff_subtitles_queue_seek(&aqt->q, s, stream_index,
|
yading@11
|
113 min_ts, ts, max_ts, flags);
|
yading@11
|
114 }
|
yading@11
|
115
|
yading@11
|
116 static int aqt_read_close(AVFormatContext *s)
|
yading@11
|
117 {
|
yading@11
|
118 AQTitleContext *aqt = s->priv_data;
|
yading@11
|
119 ff_subtitles_queue_clean(&aqt->q);
|
yading@11
|
120 return 0;
|
yading@11
|
121 }
|
yading@11
|
122
|
yading@11
|
123 #define OFFSET(x) offsetof(AQTitleContext, x)
|
yading@11
|
124 #define SD AV_OPT_FLAG_SUBTITLE_PARAM|AV_OPT_FLAG_DECODING_PARAM
|
yading@11
|
125 static const AVOption aqt_options[] = {
|
yading@11
|
126 { "subfps", "set the movie frame rate", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=25}, 0, INT_MAX, SD },
|
yading@11
|
127 { NULL }
|
yading@11
|
128 };
|
yading@11
|
129
|
yading@11
|
130 static const AVClass aqt_class = {
|
yading@11
|
131 .class_name = "aqtdec",
|
yading@11
|
132 .item_name = av_default_item_name,
|
yading@11
|
133 .option = aqt_options,
|
yading@11
|
134 .version = LIBAVUTIL_VERSION_INT,
|
yading@11
|
135 };
|
yading@11
|
136
|
yading@11
|
137 AVInputFormat ff_aqtitle_demuxer = {
|
yading@11
|
138 .name = "aqtitle",
|
yading@11
|
139 .long_name = NULL_IF_CONFIG_SMALL("AQTitle subtitles"),
|
yading@11
|
140 .priv_data_size = sizeof(AQTitleContext),
|
yading@11
|
141 .read_probe = aqt_probe,
|
yading@11
|
142 .read_header = aqt_read_header,
|
yading@11
|
143 .read_packet = aqt_read_packet,
|
yading@11
|
144 .read_seek2 = aqt_read_seek,
|
yading@11
|
145 .read_close = aqt_read_close,
|
yading@11
|
146 .extensions = "aqt",
|
yading@11
|
147 .priv_class = &aqt_class,
|
yading@11
|
148 };
|