| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * LRC lyrics file format demuxer | ||
| 3 | * Copyright (c) 2014 StarBrilliant <m13253@hotmail.com> | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 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. | ||
| 11 | * | ||
| 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. | ||
| 16 | * | ||
| 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 | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <inttypes.h> | ||
| 23 | #include <stdint.h> | ||
| 24 | #include <string.h> | ||
| 25 | |||
| 26 | #include "avformat.h" | ||
| 27 | #include "demux.h" | ||
| 28 | #include "internal.h" | ||
| 29 | #include "lrc.h" | ||
| 30 | #include "metadata.h" | ||
| 31 | #include "subtitles.h" | ||
| 32 | #include "libavutil/bprint.h" | ||
| 33 | #include "libavutil/dict.h" | ||
| 34 | |||
| 35 | typedef struct LRCContext { | ||
| 36 | FFDemuxSubtitlesQueue q; | ||
| 37 | int64_t ts_offset; // offset metadata item | ||
| 38 | } LRCContext; | ||
| 39 | |||
| 40 | 93 | static int64_t find_header(const char *p) | |
| 41 | { | ||
| 42 | 93 | int64_t offset = 0; | |
| 43 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
|
93 | while(p[offset] == ' ' || p[offset] == '\t') { |
| 44 | ✗ | offset++; | |
| 45 | } | ||
| 46 |
5/6✓ Branch 0 taken 87 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 72 times.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
|
93 | if(p[offset] == '[' && p[offset + 1] >= 'a' && p[offset + 1] <= 'z') { |
| 47 | 15 | return offset; | |
| 48 | } else { | ||
| 49 | 78 | return -1; | |
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | 78 | static int64_t count_ts(const char *p) | |
| 54 | { | ||
| 55 | 78 | int64_t offset = 0; | |
| 56 | 78 | int in_brackets = 0; | |
| 57 | |||
| 58 | for(;;) { | ||
| 59 |
2/4✓ Branch 0 taken 1457 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1457 times.
|
1457 | if(p[offset] == ' ' || p[offset] == '\t') { |
| 60 | ✗ | offset++; | |
| 61 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 1328 times.
|
1457 | } else if(p[offset] == '[') { |
| 62 | 129 | offset++; | |
| 63 | 129 | in_brackets++; | |
| 64 |
3/4✓ Branch 0 taken 129 times.
✓ Branch 1 taken 1199 times.
✓ Branch 2 taken 129 times.
✗ Branch 3 not taken.
|
1328 | } else if (p[offset] == ']' && in_brackets) { |
| 65 | 129 | offset++; | |
| 66 | 129 | in_brackets--; | |
| 67 |
2/2✓ Branch 0 taken 1121 times.
✓ Branch 1 taken 78 times.
|
1199 | } else if(in_brackets && |
| 68 |
6/6✓ Branch 0 taken 992 times.
✓ Branch 1 taken 129 times.
✓ Branch 2 taken 863 times.
✓ Branch 3 taken 129 times.
✓ Branch 4 taken 860 times.
✓ Branch 5 taken 3 times.
|
1121 | (p[offset] == ':' || p[offset] == '.' || p[offset] == '-' || |
| 69 |
2/4✓ Branch 0 taken 860 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 860 times.
✗ Branch 3 not taken.
|
860 | (p[offset] >= '0' && p[offset] <= '9'))) { |
| 70 | 1121 | offset++; | |
| 71 | } else { | ||
| 72 | break; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | 78 | return offset; | |
| 76 | } | ||
| 77 | |||
| 78 | 207 | static int64_t read_ts(const char *p, int64_t *start) | |
| 79 | { | ||
| 80 | 207 | int64_t offset = 0; | |
| 81 | uint32_t mm; | ||
| 82 | double ss; | ||
| 83 | char prefix[3]; | ||
| 84 | |||
| 85 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 207 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 207 times.
|
207 | while(p[offset] == ' ' || p[offset] == '\t') { |
| 86 | ✗ | offset++; | |
| 87 | } | ||
| 88 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 129 times.
|
207 | if(p[offset] != '[') { |
| 89 | 78 | return 0; | |
| 90 | } | ||
| 91 | 129 | int ret = av_sscanf(p, "%2[[-]%"SCNu32":%lf]", prefix, &mm, &ss); | |
| 92 |
4/8✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 129 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 129 times.
|
129 | if (ret != 3 || prefix[0] != '[' || ss < 0 || ss > 60) { |
| 93 | ✗ | return 0; | |
| 94 | } | ||
| 95 | 129 | *start = llrint((mm * 60 + ss) * AV_TIME_BASE); | |
| 96 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 126 times.
|
129 | if (prefix[1] == '-') { |
| 97 | 3 | *start = - *start; | |
| 98 | } | ||
| 99 | do { | ||
| 100 | 1379 | offset++; | |
| 101 |
3/4✓ Branch 0 taken 1379 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1250 times.
✓ Branch 3 taken 129 times.
|
1379 | } while(p[offset] && p[offset-1] != ']'); |
| 102 | 129 | return offset; | |
| 103 | } | ||
| 104 | |||
| 105 | 93 | static int64_t read_line(AVBPrint *buf, AVIOContext *pb) | |
| 106 | { | ||
| 107 | 93 | int64_t pos = avio_tell(pb); | |
| 108 | |||
| 109 | 93 | av_bprint_clear(buf); | |
| 110 |
2/2✓ Branch 1 taken 4211 times.
✓ Branch 2 taken 3 times.
|
4214 | while(!avio_feof(pb)) { |
| 111 | 4211 | int c = avio_r8(pb); | |
| 112 |
1/2✓ Branch 0 taken 4211 times.
✗ Branch 1 not taken.
|
4211 | if(c != '\r') { |
| 113 | 4211 | av_bprint_chars(buf, c, 1); | |
| 114 | } | ||
| 115 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 4121 times.
|
4211 | if(c == '\n') { |
| 116 | 90 | break; | |
| 117 | } | ||
| 118 | } | ||
| 119 | 93 | return pos; | |
| 120 | } | ||
| 121 | |||
| 122 | 7279 | static int lrc_probe(const AVProbeData *p) | |
| 123 | { | ||
| 124 | 7279 | int64_t offset = 0; | |
| 125 | int64_t mm; | ||
| 126 | uint64_t ss, cs; | ||
| 127 | const AVMetadataConv *metadata_item; | ||
| 128 | |||
| 129 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7270 times.
|
7279 | if(!memcmp(p->buf, "\xef\xbb\xbf", 3)) { // Skip UTF-8 BOM header |
| 130 | 9 | offset += 3; | |
| 131 | } | ||
| 132 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7283 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 7279 times.
|
7286 | while(p->buf[offset] == '\n' || p->buf[offset] == '\r') { |
| 133 | 7 | offset++; | |
| 134 | } | ||
| 135 |
2/2✓ Branch 0 taken 7270 times.
✓ Branch 1 taken 9 times.
|
7279 | if(p->buf[offset] != '[') { |
| 136 | 7270 | return 0; | |
| 137 | } | ||
| 138 | 9 | offset++; | |
| 139 | // Common metadata item but not exist in ff_lrc_metadata_conv | ||
| 140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if(!memcmp(p->buf + offset, "offset:", 7)) { |
| 141 | ✗ | return 40; | |
| 142 | } | ||
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if(sscanf(p->buf + offset, "%"SCNd64":%"SCNu64".%"SCNu64"]", |
| 144 | &mm, &ss, &cs) == 3) { | ||
| 145 | ✗ | return 50; | |
| 146 | } | ||
| 147 | // Metadata items exist in ff_lrc_metadata_conv | ||
| 148 | 9 | for(metadata_item = ff_lrc_metadata_conv; | |
| 149 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 6 times.
|
51 | metadata_item->native; metadata_item++) { |
| 150 | 45 | size_t metadata_item_len = strlen(metadata_item->native); | |
| 151 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 42 times.
|
45 | if(p->buf[offset + metadata_item_len] == ':' && |
| 152 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | !memcmp(p->buf + offset, metadata_item->native, metadata_item_len)) { |
| 153 | 3 | return 40; | |
| 154 | } | ||
| 155 | } | ||
| 156 | 6 | return 5; // Give it 5 scores since it starts with a bracket | |
| 157 | } | ||
| 158 | |||
| 159 | 3 | static int lrc_read_header(AVFormatContext *s) | |
| 160 | { | ||
| 161 | 3 | LRCContext *lrc = s->priv_data; | |
| 162 | AVBPrint line; | ||
| 163 | AVStream *st; | ||
| 164 | |||
| 165 | 3 | st = avformat_new_stream(s, NULL); | |
| 166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if(!st) { |
| 167 | ✗ | return AVERROR(ENOMEM); | |
| 168 | } | ||
| 169 | 3 | avpriv_set_pts_info(st, 64, 1, AV_TIME_BASE); | |
| 170 | 3 | lrc->ts_offset = 0; | |
| 171 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
| 172 | 3 | st->codecpar->codec_id = AV_CODEC_ID_TEXT; | |
| 173 | 3 | av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 174 | |||
| 175 |
2/2✓ Branch 1 taken 93 times.
✓ Branch 2 taken 3 times.
|
96 | while(!avio_feof(s->pb)) { |
| 176 | 93 | int64_t header_offset, pos = read_line(&line, s->pb); | |
| 177 | |||
| 178 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
|
93 | if (!av_bprint_is_complete(&line)) |
| 179 | ✗ | goto err_nomem_out; | |
| 180 | 93 | header_offset = find_header(line.str); | |
| 181 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 78 times.
|
93 | if(header_offset >= 0) { |
| 182 | 15 | char *comma_offset = strchr(line.str, ':'); | |
| 183 |
1/2✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
|
15 | if(comma_offset) { |
| 184 | 15 | char *right_bracket_offset = strchr(line.str, ']'); | |
| 185 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if(!right_bracket_offset) { |
| 186 | ✗ | continue; | |
| 187 | } | ||
| 188 | |||
| 189 | 15 | *right_bracket_offset = *comma_offset = '\0'; | |
| 190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if(strcmp(line.str + 1, "offset") || |
| 191 | ✗ | sscanf(comma_offset + 1, "%"SCNd64, &lrc->ts_offset) != 1) { | |
| 192 | 15 | av_dict_set(&s->metadata, line.str + 1, comma_offset + 1, 0); | |
| 193 | } | ||
| 194 | 15 | lrc->ts_offset = av_clip64(lrc->ts_offset, INT64_MIN/4, INT64_MAX/4); | |
| 195 | |||
| 196 | 15 | *comma_offset = ':'; | |
| 197 | 15 | *right_bracket_offset = ']'; | |
| 198 | } | ||
| 199 | |||
| 200 | } else { | ||
| 201 | AVPacket *sub; | ||
| 202 | 78 | int64_t ts_start = AV_NOPTS_VALUE; | |
| 203 | 78 | int64_t ts_stroffset = 0; | |
| 204 | 78 | int64_t ts_stroffset_incr = 0; | |
| 205 | 78 | int64_t ts_strlength = count_ts(line.str); | |
| 206 | |||
| 207 | 207 | while((ts_stroffset_incr = read_ts(line.str + ts_stroffset, | |
| 208 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 78 times.
|
207 | &ts_start)) != 0) { |
| 209 | 129 | ts_start = av_clip64(ts_start, INT64_MIN/4, INT64_MAX/4); | |
| 210 | 129 | ts_stroffset += ts_stroffset_incr; | |
| 211 | 129 | sub = ff_subtitles_queue_insert(&lrc->q, line.str + ts_strlength, | |
| 212 | 129 | line.len - ts_strlength, 0); | |
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 129 times.
|
129 | if (!sub) |
| 214 | ✗ | goto err_nomem_out; | |
| 215 | 129 | sub->pos = pos; | |
| 216 | 129 | sub->pts = ts_start - lrc->ts_offset; | |
| 217 | 129 | sub->duration = -1; | |
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | 3 | ff_subtitles_queue_finalize(s, &lrc->q); | |
| 222 | 3 | ff_metadata_conv_ctx(s, NULL, ff_lrc_metadata_conv); | |
| 223 | 3 | av_bprint_finalize(&line, NULL); | |
| 224 | 3 | return 0; | |
| 225 | ✗ | err_nomem_out: | |
| 226 | ✗ | av_bprint_finalize(&line, NULL); | |
| 227 | ✗ | return AVERROR(ENOMEM); | |
| 228 | } | ||
| 229 | |||
| 230 | const FFInputFormat ff_lrc_demuxer = { | ||
| 231 | .p.name = "lrc", | ||
| 232 | .p.long_name = NULL_IF_CONFIG_SMALL("LRC lyrics"), | ||
| 233 | .priv_data_size = sizeof (LRCContext), | ||
| 234 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 235 | .read_probe = lrc_probe, | ||
| 236 | .read_header = lrc_read_header, | ||
| 237 | .read_packet = ff_subtitles_read_packet, | ||
| 238 | .read_close = ff_subtitles_read_close, | ||
| 239 | .read_seek2 = ff_subtitles_read_seek | ||
| 240 | }; | ||
| 241 |