Reviewed-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
... | ... |
@@ -310,6 +310,7 @@ library: |
310 | 310 |
@tab Used by Linux Media Labs MPEG-4 PCI boards |
311 | 311 |
@item LOAS @tab @tab X |
312 | 312 |
@tab contains LATM multiplexed AAC audio |
313 |
+@item LRC @tab X @tab X |
|
313 | 314 |
@item LVF @tab @tab X |
314 | 315 |
@item LXF @tab @tab X |
315 | 316 |
@tab VR native stream format, used by Leitch/Harris' video servers. |
... | ... |
@@ -211,6 +211,8 @@ OBJS-$(CONFIG_LATM_DEMUXER) += rawdec.o |
211 | 211 |
OBJS-$(CONFIG_LATM_MUXER) += latmenc.o rawenc.o |
212 | 212 |
OBJS-$(CONFIG_LMLM4_DEMUXER) += lmlm4.o |
213 | 213 |
OBJS-$(CONFIG_LOAS_DEMUXER) += loasdec.o rawdec.o |
214 |
+OBJS-$(CONFIG_LRC_DEMUXER) += lrcdec.o lrc.o subtitles.o |
|
215 |
+OBJS-$(CONFIG_LRC_MUXER) += lrcenc.o lrc.o |
|
214 | 216 |
OBJS-$(CONFIG_LVF_DEMUXER) += lvfdec.o |
215 | 217 |
OBJS-$(CONFIG_LXF_DEMUXER) += lxfdec.o |
216 | 218 |
OBJS-$(CONFIG_M4V_DEMUXER) += m4vdec.o rawdec.o |
... | ... |
@@ -163,6 +163,7 @@ void av_register_all(void) |
163 | 163 |
REGISTER_MUXDEMUX(LATM, latm); |
164 | 164 |
REGISTER_DEMUXER (LMLM4, lmlm4); |
165 | 165 |
REGISTER_DEMUXER (LOAS, loas); |
166 |
+ REGISTER_MUXDEMUX(LRC, lrc); |
|
166 | 167 |
REGISTER_DEMUXER (LVF, lvf); |
167 | 168 |
REGISTER_DEMUXER (LXF, lxf); |
168 | 169 |
REGISTER_MUXDEMUX(M4V, m4v); |
169 | 170 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,34 @@ |
0 |
+/* |
|
1 |
+ * LRC lyrics file format decoder |
|
2 |
+ * Copyright (c) 2014 StarBrilliant <m13253@hotmail.com> |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#include "metadata.h" |
|
22 |
+#include "lrc.h" |
|
23 |
+ |
|
24 |
+const AVMetadataConv ff_lrc_metadata_conv[] = { |
|
25 |
+ {"ti", "title"}, |
|
26 |
+ {"al", "album"}, |
|
27 |
+ {"ar", "artist"}, |
|
28 |
+ {"au", "author"}, |
|
29 |
+ {"by", "creator"}, |
|
30 |
+ {"re", "encoder"}, |
|
31 |
+ {"ve", "encoder_version"}, |
|
32 |
+ {0, 0} |
|
33 |
+}; |
0 | 34 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,29 @@ |
0 |
+/* |
|
1 |
+ * LRC lyrics file format decoder |
|
2 |
+ * Copyright (c) 2014 StarBrilliant <m13253@hotmail.com> |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#ifndef AVFORMAT_LRC_H |
|
22 |
+#define AVFORMAT_LRC_H |
|
23 |
+ |
|
24 |
+#include "metadata.h" |
|
25 |
+ |
|
26 |
+extern const AVMetadataConv ff_lrc_metadata_conv[]; |
|
27 |
+ |
|
28 |
+#endif |
0 | 29 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,248 @@ |
0 |
+/* |
|
1 |
+ * LRC lyrics file format decoder |
|
2 |
+ * Copyright (c) 2014 StarBrilliant <m13253@hotmail.com> |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#include <inttypes.h> |
|
22 |
+#include <stdint.h> |
|
23 |
+#include <string.h> |
|
24 |
+ |
|
25 |
+#include "avformat.h" |
|
26 |
+#include "internal.h" |
|
27 |
+#include "lrc.h" |
|
28 |
+#include "metadata.h" |
|
29 |
+#include "subtitles.h" |
|
30 |
+#include "libavutil/bprint.h" |
|
31 |
+#include "libavutil/dict.h" |
|
32 |
+ |
|
33 |
+typedef struct LRCContext { |
|
34 |
+ FFDemuxSubtitlesQueue q; |
|
35 |
+ int64_t ts_offset; // offset metadata item |
|
36 |
+} LRCContext; |
|
37 |
+ |
|
38 |
+static int64_t find_header(const char *p) |
|
39 |
+{ |
|
40 |
+ int64_t offset = 0; |
|
41 |
+ while(p[offset] == ' ' || p[offset] == '\t') { |
|
42 |
+ offset++; |
|
43 |
+ } |
|
44 |
+ if(p[offset] == '[' && p[offset + 1] >= 'a' && p[offset + 1] <= 'z') { |
|
45 |
+ return offset; |
|
46 |
+ } else { |
|
47 |
+ return -1; |
|
48 |
+ } |
|
49 |
+} |
|
50 |
+ |
|
51 |
+static int64_t count_ts(const char *p) |
|
52 |
+{ |
|
53 |
+ int64_t offset = 0; |
|
54 |
+ int in_brackets = 0; |
|
55 |
+ |
|
56 |
+ for(;;) { |
|
57 |
+ if(p[offset] == ' ' || p[offset] == '\t') { |
|
58 |
+ offset++; |
|
59 |
+ } else if(p[offset] == '[') { |
|
60 |
+ offset++; |
|
61 |
+ in_brackets++; |
|
62 |
+ } else if (p[offset] == ']' && in_brackets) { |
|
63 |
+ offset++; |
|
64 |
+ in_brackets--; |
|
65 |
+ } else if(in_brackets && |
|
66 |
+ (p[offset] == ':' || p[offset] == '.' || p[offset] == '-' || |
|
67 |
+ (p[offset] >= '0' && p[offset] <= '9'))) { |
|
68 |
+ offset++; |
|
69 |
+ } else { |
|
70 |
+ break; |
|
71 |
+ } |
|
72 |
+ } |
|
73 |
+ return offset; |
|
74 |
+} |
|
75 |
+ |
|
76 |
+static int64_t read_ts(const char *p, int64_t *start) |
|
77 |
+{ |
|
78 |
+ int64_t offset = 0; |
|
79 |
+ uint64_t mm, ss, cs; |
|
80 |
+ |
|
81 |
+ while(p[offset] == ' ' || p[offset] == '\t') { |
|
82 |
+ offset++; |
|
83 |
+ } |
|
84 |
+ if(p[offset] != '[') { |
|
85 |
+ return 0; |
|
86 |
+ } |
|
87 |
+ if(sscanf(p, "[-%"SCNu64":%"SCNu64".%"SCNu64"]", &mm, &ss, &cs) == 3) { |
|
88 |
+ /* Just in case negative pts, players may drop it but we won't. */ |
|
89 |
+ *start = -(int64_t) (mm*60000 + ss*1000 + cs*10); |
|
90 |
+ } else if(sscanf(p, "[%"SCNu64":%"SCNu64".%"SCNu64"]", &mm, &ss, &cs) == 3) { |
|
91 |
+ *start = mm*60000 + ss*1000 + cs*10; |
|
92 |
+ } else { |
|
93 |
+ return 0; |
|
94 |
+ } |
|
95 |
+ do { |
|
96 |
+ offset++; |
|
97 |
+ } while(p[offset] && p[offset-1] != ']'); |
|
98 |
+ return offset; |
|
99 |
+} |
|
100 |
+ |
|
101 |
+static int64_t read_line(AVBPrint *buf, AVIOContext *pb) |
|
102 |
+{ |
|
103 |
+ int64_t pos = avio_tell(pb); |
|
104 |
+ |
|
105 |
+ av_bprint_clear(buf); |
|
106 |
+ while(!url_feof(pb)) { |
|
107 |
+ int c = avio_r8(pb); |
|
108 |
+ if(c != '\r') { |
|
109 |
+ av_bprint_chars(buf, c, 1); |
|
110 |
+ } |
|
111 |
+ if(c == '\n') { |
|
112 |
+ break; |
|
113 |
+ } |
|
114 |
+ } |
|
115 |
+ return pos; |
|
116 |
+} |
|
117 |
+ |
|
118 |
+static int lrc_probe(AVProbeData *p) |
|
119 |
+{ |
|
120 |
+ int64_t offset = 0; |
|
121 |
+ int64_t mm; |
|
122 |
+ uint64_t ss, cs; |
|
123 |
+ const AVMetadataConv *metadata_item; |
|
124 |
+ |
|
125 |
+ if(!memcmp(p->buf, "\xef\xbb\xbf", 3)) { // Skip UTF-8 BOM header |
|
126 |
+ offset += 3; |
|
127 |
+ } |
|
128 |
+ while(p->buf[offset] == '\n' || p->buf[offset] == '\r') { |
|
129 |
+ offset++; |
|
130 |
+ } |
|
131 |
+ if(p->buf[offset] != '[') { |
|
132 |
+ return 0; |
|
133 |
+ } |
|
134 |
+ offset++; |
|
135 |
+ // Common metadata item but not exist in ff_lrc_metadata_conv |
|
136 |
+ if(!memcmp(p->buf + offset, "offset:", 7)) { |
|
137 |
+ return 40; |
|
138 |
+ } |
|
139 |
+ if(sscanf(p->buf + offset, "%"SCNd64":%"SCNu64".%"SCNu64"]", |
|
140 |
+ &mm, &ss, &cs) == 3) { |
|
141 |
+ return 50; |
|
142 |
+ } |
|
143 |
+ // Metadata items exist in ff_lrc_metadata_conv |
|
144 |
+ for(metadata_item = ff_lrc_metadata_conv; |
|
145 |
+ metadata_item->native; metadata_item++) { |
|
146 |
+ size_t metadata_item_len = strlen(metadata_item->native); |
|
147 |
+ if(p->buf[offset + metadata_item_len] == ':' && |
|
148 |
+ !memcmp(p->buf + offset, metadata_item->native, metadata_item_len)) { |
|
149 |
+ return 40; |
|
150 |
+ } |
|
151 |
+ } |
|
152 |
+ return 5; // Give it 5 scores since it starts with a bracket |
|
153 |
+} |
|
154 |
+ |
|
155 |
+static int lrc_read_header(AVFormatContext *s) |
|
156 |
+{ |
|
157 |
+ LRCContext *lrc = s->priv_data; |
|
158 |
+ AVBPrint line; |
|
159 |
+ AVStream *st; |
|
160 |
+ |
|
161 |
+ st = avformat_new_stream(s, NULL); |
|
162 |
+ if(!st) { |
|
163 |
+ return AVERROR(ENOMEM); |
|
164 |
+ } |
|
165 |
+ avpriv_set_pts_info(st, 64, 1, 1000); |
|
166 |
+ lrc->ts_offset = 0; |
|
167 |
+ st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
|
168 |
+ st->codec->codec_id = AV_CODEC_ID_TEXT; |
|
169 |
+ av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED); |
|
170 |
+ |
|
171 |
+ while(!url_feof(s->pb)) { |
|
172 |
+ int64_t pos = read_line(&line, s->pb); |
|
173 |
+ int64_t header_offset = find_header(line.str); |
|
174 |
+ if(header_offset >= 0) { |
|
175 |
+ char *comma_offset = strchr(line.str, ':'); |
|
176 |
+ if(comma_offset) { |
|
177 |
+ char *right_bracket_offset = strchr(line.str, ']'); |
|
178 |
+ if(!right_bracket_offset) { |
|
179 |
+ continue; |
|
180 |
+ } |
|
181 |
+ |
|
182 |
+ *right_bracket_offset = *comma_offset = '\0'; |
|
183 |
+ if(strcmp(line.str + 1, "offset") || |
|
184 |
+ sscanf(comma_offset + 1, "%"SCNd64, &lrc->ts_offset) != 1) { |
|
185 |
+ av_dict_set(&s->metadata, line.str + 1, comma_offset + 1, 0); |
|
186 |
+ } |
|
187 |
+ *comma_offset = ':'; |
|
188 |
+ *right_bracket_offset = ']'; |
|
189 |
+ } |
|
190 |
+ |
|
191 |
+ } else { |
|
192 |
+ AVPacket *sub; |
|
193 |
+ int64_t ts_start = AV_NOPTS_VALUE; |
|
194 |
+ int64_t ts_stroffset = 0; |
|
195 |
+ int64_t ts_stroffset_incr = 0; |
|
196 |
+ int64_t ts_strlength = count_ts(line.str); |
|
197 |
+ |
|
198 |
+ while((ts_stroffset_incr = read_ts(line.str + ts_stroffset, |
|
199 |
+ &ts_start)) != 0) { |
|
200 |
+ ts_stroffset += ts_stroffset_incr; |
|
201 |
+ sub = ff_subtitles_queue_insert(&lrc->q, line.str + ts_strlength, |
|
202 |
+ line.len - ts_strlength, 0); |
|
203 |
+ if(!sub) { |
|
204 |
+ return AVERROR(ENOMEM); |
|
205 |
+ } |
|
206 |
+ sub->pos = pos; |
|
207 |
+ sub->pts = ts_start - lrc->ts_offset; |
|
208 |
+ sub->duration = -1; |
|
209 |
+ } |
|
210 |
+ } |
|
211 |
+ } |
|
212 |
+ ff_subtitles_queue_finalize(&lrc->q); |
|
213 |
+ ff_metadata_conv_ctx(s, NULL, ff_lrc_metadata_conv); |
|
214 |
+ return 0; |
|
215 |
+} |
|
216 |
+ |
|
217 |
+static int lrc_read_packet(AVFormatContext *s, AVPacket *pkt) |
|
218 |
+{ |
|
219 |
+ LRCContext *lrc = s->priv_data; |
|
220 |
+ return ff_subtitles_queue_read_packet(&lrc->q, pkt); |
|
221 |
+} |
|
222 |
+ |
|
223 |
+static int lrc_read_seek(AVFormatContext *s, int stream_index, |
|
224 |
+ int64_t min_ts, int64_t ts, int64_t max_ts, int flags) |
|
225 |
+{ |
|
226 |
+ LRCContext *lrc = s->priv_data; |
|
227 |
+ return ff_subtitles_queue_seek(&lrc->q, s, stream_index, |
|
228 |
+ min_ts, ts, max_ts, flags); |
|
229 |
+} |
|
230 |
+ |
|
231 |
+static int lrc_read_close(AVFormatContext *s) |
|
232 |
+{ |
|
233 |
+ LRCContext *lrc = s->priv_data; |
|
234 |
+ ff_subtitles_queue_clean(&lrc->q); |
|
235 |
+ return 0; |
|
236 |
+} |
|
237 |
+ |
|
238 |
+AVInputFormat ff_lrc_demuxer = { |
|
239 |
+ .name = "lrc", |
|
240 |
+ .long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"), |
|
241 |
+ .priv_data_size = sizeof (LRCContext), |
|
242 |
+ .read_probe = lrc_probe, |
|
243 |
+ .read_header = lrc_read_header, |
|
244 |
+ .read_packet = lrc_read_packet, |
|
245 |
+ .read_close = lrc_read_close, |
|
246 |
+ .read_seek2 = lrc_read_seek |
|
247 |
+}; |
0 | 248 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,152 @@ |
0 |
+/* |
|
1 |
+ * LRC lyrics file format decoder |
|
2 |
+ * Copyright (c) 2014 StarBrilliant <m13253@hotmail.com> |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#include <inttypes.h> |
|
22 |
+#include <stdint.h> |
|
23 |
+#include <string.h> |
|
24 |
+ |
|
25 |
+#include "avformat.h" |
|
26 |
+#include "internal.h" |
|
27 |
+#include "lrc.h" |
|
28 |
+#include "metadata.h" |
|
29 |
+#include "subtitles.h" |
|
30 |
+#include "version.h" |
|
31 |
+#include "libavutil/bprint.h" |
|
32 |
+#include "libavutil/dict.h" |
|
33 |
+#include "libavutil/log.h" |
|
34 |
+#include "libavutil/macros.h" |
|
35 |
+ |
|
36 |
+static int lrc_write_header(AVFormatContext *s) |
|
37 |
+{ |
|
38 |
+ const AVDictionaryEntry *metadata_item; |
|
39 |
+ |
|
40 |
+ if(s->nb_streams != 1 || |
|
41 |
+ s->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) { |
|
42 |
+ av_log(s, AV_LOG_ERROR, |
|
43 |
+ "LRC supports only a single subtitle stream.\n"); |
|
44 |
+ return AVERROR(EINVAL); |
|
45 |
+ } |
|
46 |
+ if(s->streams[0]->codec->codec_id != AV_CODEC_ID_SUBRIP && |
|
47 |
+ s->streams[0]->codec->codec_id != AV_CODEC_ID_TEXT) { |
|
48 |
+ av_log(s, AV_LOG_ERROR, "Unsupported subtitle codec: %s\n", |
|
49 |
+ avcodec_get_name(s->streams[0]->codec->codec_id)); |
|
50 |
+ return AVERROR(EINVAL); |
|
51 |
+ } |
|
52 |
+ avpriv_set_pts_info(s->streams[0], 64, 1, 100); |
|
53 |
+ |
|
54 |
+ ff_metadata_conv_ctx(s, ff_lrc_metadata_conv, NULL); |
|
55 |
+ if(!(s->flags & AVFMT_FLAG_BITEXACT)) { // avoid breaking regression tests |
|
56 |
+ /* LRC provides a metadata slot for specifying encoder version |
|
57 |
+ * in addition to encoder name. We will store LIBAVFORMAT_VERSION |
|
58 |
+ * to it. |
|
59 |
+ */ |
|
60 |
+ av_dict_set(&s->metadata, "ve", AV_STRINGIFY(LIBAVFORMAT_VERSION), 0); |
|
61 |
+ } else { |
|
62 |
+ av_dict_set(&s->metadata, "ve", NULL, 0); |
|
63 |
+ } |
|
64 |
+ for(metadata_item = NULL; |
|
65 |
+ (metadata_item = av_dict_get(s->metadata, "", metadata_item, |
|
66 |
+ AV_DICT_IGNORE_SUFFIX)) != NULL;) { |
|
67 |
+ char *delim; |
|
68 |
+ if(!metadata_item->value[0]) { |
|
69 |
+ continue; |
|
70 |
+ } |
|
71 |
+ while((delim = strchr(metadata_item->value, '\n')) != NULL) { |
|
72 |
+ *delim = ' '; |
|
73 |
+ } |
|
74 |
+ while((delim = strchr(metadata_item->value, '\r')) != NULL) { |
|
75 |
+ *delim = ' '; |
|
76 |
+ } |
|
77 |
+ avio_printf(s->pb, "[%s:%s]\n", |
|
78 |
+ metadata_item->key, metadata_item->value); |
|
79 |
+ } |
|
80 |
+ avio_printf(s->pb, "\n"); |
|
81 |
+ return 0; |
|
82 |
+} |
|
83 |
+ |
|
84 |
+static int lrc_write_packet(AVFormatContext *s, AVPacket *pkt) |
|
85 |
+{ |
|
86 |
+ if(pkt->pts != AV_NOPTS_VALUE) { |
|
87 |
+ char *data = av_malloc(pkt->size + 1); |
|
88 |
+ char *line; |
|
89 |
+ char *delim; |
|
90 |
+ |
|
91 |
+ if(!data) { |
|
92 |
+ return AVERROR(ENOMEM); |
|
93 |
+ } |
|
94 |
+ memcpy(data, pkt->data, pkt->size); |
|
95 |
+ data[pkt->size] = '\0'; |
|
96 |
+ |
|
97 |
+ for(delim = data + pkt->size - 1; |
|
98 |
+ delim >= data && (delim[0] == '\n' || delim[0] == '\r'); delim--) { |
|
99 |
+ delim[0] = '\0'; // Strip last empty lines |
|
100 |
+ } |
|
101 |
+ line = data; |
|
102 |
+ while(line[0] == '\n' || line[0] == '\r') { |
|
103 |
+ line++; // Skip first empty lines |
|
104 |
+ } |
|
105 |
+ |
|
106 |
+ while(line) { |
|
107 |
+ delim = strchr(line, '\n'); |
|
108 |
+ if(delim) { |
|
109 |
+ if(delim > line && delim[-1] == '\r') { |
|
110 |
+ delim[-1] = '\0'; |
|
111 |
+ } |
|
112 |
+ delim[0] = '\0'; |
|
113 |
+ delim++; |
|
114 |
+ } |
|
115 |
+ if(line[0] == '[') { |
|
116 |
+ av_log(s, AV_LOG_WARNING, |
|
117 |
+ "Subtitle starts with '[', may cause problems with LRC format.\n"); |
|
118 |
+ } |
|
119 |
+ |
|
120 |
+ if(pkt->pts >= 0) { |
|
121 |
+ avio_printf(s->pb, "[%02"PRId64":%02"PRId64".%02"PRId64"]", |
|
122 |
+ (pkt->pts / 6000), |
|
123 |
+ ((pkt->pts / 100) % 60), |
|
124 |
+ (pkt->pts % 100)); |
|
125 |
+ } else { |
|
126 |
+ /* Offset feature of LRC can easily make pts negative, |
|
127 |
+ * we just output it directly and let the player drop it. */ |
|
128 |
+ avio_printf(s->pb, "[-%02"PRId64":%02"PRId64".%02"PRId64"]", |
|
129 |
+ (-pkt->pts) / 6000, |
|
130 |
+ ((-pkt->pts) / 100) % 60, |
|
131 |
+ (-pkt->pts) % 100); |
|
132 |
+ } |
|
133 |
+ avio_printf(s->pb, "%s\n", line); |
|
134 |
+ line = delim; |
|
135 |
+ } |
|
136 |
+ av_free(data); |
|
137 |
+ } |
|
138 |
+ return 0; |
|
139 |
+} |
|
140 |
+ |
|
141 |
+AVOutputFormat ff_lrc_muxer = { |
|
142 |
+ .name = "lrc", |
|
143 |
+ .long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"), |
|
144 |
+ .extensions = "lrc", |
|
145 |
+ .priv_data_size = 0, |
|
146 |
+ .write_header = lrc_write_header, |
|
147 |
+ .write_packet = lrc_write_packet, |
|
148 |
+ .flags = AVFMT_VARIABLE_FPS | AVFMT_GLOBALHEADER | |
|
149 |
+ AVFMT_TS_NEGATIVE | AVFMT_TS_NONSTRICT, |
|
150 |
+ .subtitle_codec = AV_CODEC_ID_SUBRIP |
|
151 |
+}; |