FFmpeg coverage


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