FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/ffmetadec.c
Date: 2024-03-29 01:21:52
Exec Total Coverage
Lines: 66 117 56.4%
Functions: 6 9 66.7%
Branches: 47 98 48.0%

Line Branch Exec Source
1 /*
2 * Metadata demuxer
3 * Copyright (c) 2010 Anton Khirnov
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 "libavutil/bprint.h"
23 #include "libavutil/mathematics.h"
24 #include "avformat.h"
25 #include "demux.h"
26 #include "ffmeta.h"
27 #include "libavutil/dict.h"
28
29 7124 static int probe(const AVProbeData *p)
30 {
31
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7124 times.
7124 if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
32 return AVPROBE_SCORE_MAX;
33 7124 return 0;
34 }
35
36 10 static int64_t read_line_to_bprint_escaped(AVIOContext *s, AVBPrint *bp)
37 {
38 int len, end;
39 10 int64_t read = 0;
40 char tmp[1024];
41 char c;
42 10 char prev = ' ';
43
44 do {
45 10 len = 0;
46 do {
47 224 c = avio_r8(s);
48
7/8
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 213 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 212 times.
224 end = prev != '\\' && (c == '\r' || c == '\n' || c == '\0');
49
2/2
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 10 times.
224 if (!end)
50 214 tmp[len++] = c;
51 224 prev = c;
52
3/4
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 214 times.
✗ Branch 3 not taken.
224 } while (!end && len < sizeof(tmp));
53 10 av_bprint_append_data(bp, tmp, len);
54 10 read += len;
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 } while (!end);
56
57
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
10 if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
58 avio_skip(s, -1);
59
60
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
10 if (!c && s->error)
61 return s->error;
62
63
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
10 if (!c && !read && avio_feof(s))
64 1 return AVERROR_EOF;
65
66 9 return read;
67 }
68
69 9 static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
70 {
71
72 do {
73 10 av_bprint_clear(bp);
74 10 read_line_to_bprint_escaped(s, bp);
75
6/8
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
10 } while (!avio_feof(s) && (bp->str[0] == ';' || bp->str[0] == '#' || bp->str[0] == 0));
76 9 }
77
78 static void get_line(AVIOContext *s, uint8_t *buf, int size)
79 {
80 do {
81 uint8_t c;
82 int i = 0;
83
84 while ((c = avio_r8(s))) {
85 if (c == '\\') {
86 if (i < size - 1)
87 buf[i++] = c;
88 c = avio_r8(s);
89 } else if (c == '\n')
90 break;
91
92 if (i < size - 1)
93 buf[i++] = c;
94 }
95 buf[i] = 0;
96 } while (!avio_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
97 }
98
99 static AVChapter *read_chapter(AVFormatContext *s)
100 {
101 uint8_t line[256];
102 int64_t start, end;
103 AVRational tb = {1, 1e9};
104 int ret;
105
106 get_line(s->pb, line, sizeof(line));
107
108 if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
109 get_line(s->pb, line, sizeof(line));
110 ret = sscanf(line, "START=%"SCNd64, &start);
111 if (ret <= 0) {
112 av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
113 start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
114 s->chapters[s->nb_chapters - 1]->end : 0;
115 } else
116 get_line(s->pb, line, sizeof(line));
117
118 ret = sscanf(line, "END=%"SCNd64, &end);
119 if (ret <= 0) {
120 av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
121 end = AV_NOPTS_VALUE;
122 }
123
124 return avpriv_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
125 }
126
127 12 static uint8_t *unescape(const uint8_t *buf, int size)
128 {
129 12 uint8_t *ret = av_malloc(size + 1);
130 12 uint8_t *p1 = ret;
131 12 const uint8_t *p2 = buf;
132
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!ret)
134 return NULL;
135
136
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 12 times.
190 while (p2 < buf + size) {
137
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 176 times.
178 if (*p2 == '\\')
138 2 p2++;
139 178 *p1++ = *p2++;
140 }
141 12 *p1 = 0;
142 12 return ret;
143 }
144
145 7 static int read_tag(const uint8_t *line, AVDictionary **m)
146 {
147 uint8_t *key, *value;
148 7 const uint8_t *p = line;
149
150 /* find first not escaped '=' */
151 while (1) {
152
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 38 times.
44 if (*p == '=')
153 6 break;
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 else if (*p == '\\')
155 p++;
156
157
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1 times.
38 if (*p++)
158 37 continue;
159
160 1 return 0;
161 }
162
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!(key = unescape(line, p - line)))
164 return AVERROR(ENOMEM);
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!(value = unescape(p + 1, strlen(p + 1)))) {
166 av_free(key);
167 return AVERROR(ENOMEM);
168 }
169
170 6 av_dict_set(m, key, value, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
171 6 return 0;
172 }
173
174 1 static int read_header(AVFormatContext *s)
175 {
176 1 AVDictionary **m = &s->metadata;
177 AVBPrint bp;
178
179 1 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
180
181
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 times.
10 while(!avio_feof(s->pb)) {
182 9 get_bprint_line(s->pb, &bp);
183
184
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (!memcmp(bp.str, ID_STREAM, strlen(ID_STREAM))) {
185 2 AVStream *st = avformat_new_stream(s, NULL);
186
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st)
188 goto nomem;
189
190 2 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
191 2 st->codecpar->codec_id = AV_CODEC_ID_FFMETADATA;
192
193 2 m = &st->metadata;
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 } else if (!memcmp(bp.str, ID_CHAPTER, strlen(ID_CHAPTER))) {
195 AVChapter *ch = read_chapter(s);
196
197 if (!ch)
198 goto nomem;
199
200 m = &ch->metadata;
201 } else
202 7 read_tag(bp.str, m);
203 }
204
205 1 av_bprint_finalize(&bp, NULL);
206
207 1 s->start_time = 0;
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->nb_chapters)
209 s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
210 s->chapters[s->nb_chapters - 1]->time_base,
211 AV_TIME_BASE_Q);
212
213 1 return 0;
214 nomem:
215 av_bprint_finalize(&bp, NULL);
216
217 return AVERROR(ENOMEM);
218 }
219
220 static int read_packet(AVFormatContext *s, AVPacket *pkt)
221 {
222 return AVERROR_EOF;
223 }
224
225 const FFInputFormat ff_ffmetadata_demuxer = {
226 .p.name = "ffmetadata",
227 .p.long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
228 .read_probe = probe,
229 .read_header = read_header,
230 .read_packet = read_packet,
231 };
232