FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/lrcdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 103 117 88.0%
Functions: 6 6 100.0%
Branches: 66 90 73.3%

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