FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/lrcdec.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 102 112 91.1%
Functions: 6 6 100.0%
Branches: 65 88 73.9%

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