2 * LRC lyrics file format decoder
3 * Copyright (c) 2014 StarBrilliant <m13253@hotmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
30 #include "subtitles.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/dict.h"
34 typedef struct LRCContext {
35 FFDemuxSubtitlesQueue q;
36 int64_t ts_offset; // offset metadata item
39 static int64_t find_header(const char *p)
42 while(p[offset] == ' ' || p[offset] == '\t') {
45 if(p[offset] == '[' && p[offset + 1] >= 'a' && p[offset + 1] <= 'z') {
52 static int64_t count_ts(const char *p)
58 if(p[offset] == ' ' || p[offset] == '\t') {
60 } else if(p[offset] == '[') {
63 } else if (p[offset] == ']' && in_brackets) {
66 } else if(in_brackets &&
67 (p[offset] == ':' || p[offset] == '.' || p[offset] == '-' ||
68 (p[offset] >= '0' && p[offset] <= '9'))) {
77 static int64_t read_ts(const char *p, int64_t *start)
82 while(p[offset] == ' ' || p[offset] == '\t') {
85 if(p[offset] != '[') {
88 if(sscanf(p, "[-%"SCNu64":%"SCNu64".%"SCNu64"]", &mm, &ss, &cs) == 3) {
89 /* Just in case negative pts, players may drop it but we won't. */
90 *start = -(int64_t) (mm*60000 + ss*1000 + cs*10);
91 } else if(sscanf(p, "[%"SCNu64":%"SCNu64".%"SCNu64"]", &mm, &ss, &cs) == 3) {
92 *start = mm*60000 + ss*1000 + cs*10;
98 } while(p[offset] && p[offset-1] != ']');
102 static int64_t read_line(AVBPrint *buf, AVIOContext *pb)
104 int64_t pos = avio_tell(pb);
106 av_bprint_clear(buf);
107 while(!avio_feof(pb)) {
110 av_bprint_chars(buf, c, 1);
119 static int lrc_probe(const AVProbeData *p)
124 const AVMetadataConv *metadata_item;
126 if(!memcmp(p->buf, "\xef\xbb\xbf", 3)) { // Skip UTF-8 BOM header
129 while(p->buf[offset] == '\n' || p->buf[offset] == '\r') {
132 if(p->buf[offset] != '[') {
136 // Common metadata item but not exist in ff_lrc_metadata_conv
137 if(!memcmp(p->buf + offset, "offset:", 7)) {
140 if(sscanf(p->buf + offset, "%"SCNd64":%"SCNu64".%"SCNu64"]",
141 &mm, &ss, &cs) == 3) {
144 // Metadata items exist in ff_lrc_metadata_conv
145 for(metadata_item = ff_lrc_metadata_conv;
146 metadata_item->native; metadata_item++) {
147 size_t metadata_item_len = strlen(metadata_item->native);
148 if(p->buf[offset + metadata_item_len] == ':' &&
149 !memcmp(p->buf + offset, metadata_item->native, metadata_item_len)) {
153 return 5; // Give it 5 scores since it starts with a bracket
156 static int lrc_read_header(AVFormatContext *s)
158 LRCContext *lrc = s->priv_data;
162 st = avformat_new_stream(s, NULL);
164 return AVERROR(ENOMEM);
166 avpriv_set_pts_info(st, 64, 1, 1000);
168 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
169 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
170 av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
172 while(!avio_feof(s->pb)) {
173 int64_t pos = read_line(&line, s->pb);
174 int64_t header_offset = find_header(line.str);
175 if(header_offset >= 0) {
176 char *comma_offset = strchr(line.str, ':');
178 char *right_bracket_offset = strchr(line.str, ']');
179 if(!right_bracket_offset) {
183 *right_bracket_offset = *comma_offset = '\0';
184 if(strcmp(line.str + 1, "offset") ||
185 sscanf(comma_offset + 1, "%"SCNd64, &lrc->ts_offset) != 1) {
186 av_dict_set(&s->metadata, line.str + 1, comma_offset + 1, 0);
188 lrc->ts_offset = av_clip64(lrc->ts_offset, INT64_MIN/4, INT64_MAX/4);
191 *right_bracket_offset = ']';
196 int64_t ts_start = AV_NOPTS_VALUE;
197 int64_t ts_stroffset = 0;
198 int64_t ts_stroffset_incr = 0;
199 int64_t ts_strlength = count_ts(line.str);
201 while((ts_stroffset_incr = read_ts(line.str + ts_stroffset,
203 ts_start = av_clip64(ts_start, INT64_MIN/4, INT64_MAX/4);
204 ts_stroffset += ts_stroffset_incr;
205 sub = ff_subtitles_queue_insert(&lrc->q, line.str + ts_strlength,
206 line.len - ts_strlength, 0);
208 ff_subtitles_queue_clean(&lrc->q);
209 return AVERROR(ENOMEM);
212 sub->pts = ts_start - lrc->ts_offset;
217 ff_subtitles_queue_finalize(s, &lrc->q);
218 ff_metadata_conv_ctx(s, NULL, ff_lrc_metadata_conv);
219 av_bprint_finalize(&line, NULL);
223 static int lrc_read_packet(AVFormatContext *s, AVPacket *pkt)
225 LRCContext *lrc = s->priv_data;
226 return ff_subtitles_queue_read_packet(&lrc->q, pkt);
229 static int lrc_read_seek(AVFormatContext *s, int stream_index,
230 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
232 LRCContext *lrc = s->priv_data;
233 return ff_subtitles_queue_seek(&lrc->q, s, stream_index,
234 min_ts, ts, max_ts, flags);
237 static int lrc_read_close(AVFormatContext *s)
239 LRCContext *lrc = s->priv_data;
240 ff_subtitles_queue_clean(&lrc->q);
244 AVInputFormat ff_lrc_demuxer = {
246 .long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"),
247 .priv_data_size = sizeof (LRCContext),
248 .read_probe = lrc_probe,
249 .read_header = lrc_read_header,
250 .read_packet = lrc_read_packet,
251 .read_close = lrc_read_close,
252 .read_seek2 = lrc_read_seek