FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/id3v2.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 471 662 71.1%
Functions: 32 33 97.0%
Branches: 264 452 58.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2003 Fabrice Bellard
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * ID3v2 header parser
24 *
25 * Specifications available at:
26 * http://id3.org/Developer_Information
27 */
28
29 #include "config.h"
30
31 #if CONFIG_ZLIB
32 #include <zlib.h>
33 #endif
34
35 #include "libavutil/avstring.h"
36 #include "libavutil/bprint.h"
37 #include "libavutil/dict.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavcodec/png.h"
40 #include "avio_internal.h"
41 #include "demux.h"
42 #include "id3v1.h"
43 #include "id3v2.h"
44
45 const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
46 { "TALB", "album" },
47 { "TCOM", "composer" },
48 { "TCON", "genre" },
49 { "TCOP", "copyright" },
50 { "TENC", "encoded_by" },
51 { "TIT2", "title" },
52 { "TLAN", "language" },
53 { "TPE1", "artist" },
54 { "TPE2", "album_artist" },
55 { "TPE3", "performer" },
56 { "TPOS", "disc" },
57 { "TPUB", "publisher" },
58 { "TRCK", "track" },
59 { "TSSE", "encoder" },
60 { "USLT", "lyrics" },
61 { 0 }
62 };
63
64 const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
65 { "TCMP", "compilation" },
66 { "TDRC", "date" },
67 { "TDRL", "date" },
68 { "TDEN", "creation_time" },
69 { "TSOA", "album-sort" },
70 { "TSOP", "artist-sort" },
71 { "TSOT", "title-sort" },
72 { "TIT1", "grouping" },
73 { 0 }
74 };
75
76 static const AVMetadataConv id3v2_2_metadata_conv[] = {
77 { "TAL", "album" },
78 { "TCO", "genre" },
79 { "TCP", "compilation" },
80 { "TT2", "title" },
81 { "TEN", "encoded_by" },
82 { "TP1", "artist" },
83 { "TP2", "album_artist" },
84 { "TP3", "performer" },
85 { "TRK", "track" },
86 { 0 }
87 };
88
89 const char ff_id3v2_tags[][4] = {
90 "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
91 "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
92 "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
93 "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
94 { 0 },
95 };
96
97 const char ff_id3v2_4_tags[][4] = {
98 "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
99 "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
100 { 0 },
101 };
102
103 const char ff_id3v2_3_tags[][4] = {
104 "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
105 { 0 },
106 };
107
108 const char * const ff_id3v2_picture_types[21] = {
109 "Other",
110 "32x32 pixels 'file icon'",
111 "Other file icon",
112 "Cover (front)",
113 "Cover (back)",
114 "Leaflet page",
115 "Media (e.g. label side of CD)",
116 "Lead artist/lead performer/soloist",
117 "Artist/performer",
118 "Conductor",
119 "Band/Orchestra",
120 "Composer",
121 "Lyricist/text writer",
122 "Recording Location",
123 "During recording",
124 "During performance",
125 "Movie/video screen capture",
126 "A bright coloured fish",
127 "Illustration",
128 "Band/artist logotype",
129 "Publisher/Studio logotype",
130 };
131
132 const CodecMime ff_id3v2_mime_tags[] = {
133 { "image/gif", AV_CODEC_ID_GIF },
134 { "image/jpeg", AV_CODEC_ID_MJPEG },
135 { "image/jpg", AV_CODEC_ID_MJPEG },
136 { "image/png", AV_CODEC_ID_PNG },
137 { "image/tiff", AV_CODEC_ID_TIFF },
138 { "image/bmp", AV_CODEC_ID_BMP },
139 { "JPG", AV_CODEC_ID_MJPEG }, /* ID3v2.2 */
140 { "PNG", AV_CODEC_ID_PNG }, /* ID3v2.2 */
141 { "", AV_CODEC_ID_NONE },
142 };
143
144 25823 int ff_id3v2_match(const uint8_t *buf, const char *magic)
145 {
146 26118 return buf[0] == magic[0] &&
147
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 177 times.
295 buf[1] == magic[1] &&
148
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 buf[2] == magic[2] &&
149
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 buf[3] != 0xff &&
150
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 buf[4] != 0xff &&
151
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 (buf[6] & 0x80) == 0 &&
152
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 (buf[7] & 0x80) == 0 &&
153
3/4
✓ Branch 0 taken 295 times.
✓ Branch 1 taken 25528 times.
✓ Branch 2 taken 118 times.
✗ Branch 3 not taken.
26236 (buf[8] & 0x80) == 0 &&
154
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 (buf[9] & 0x80) == 0;
155 }
156
157 81 int ff_id3v2_tag_len(const uint8_t *buf)
158 {
159 81 int len = ((buf[6] & 0x7f) << 21) +
160 81 ((buf[7] & 0x7f) << 14) +
161 81 ((buf[8] & 0x7f) << 7) +
162 81 (buf[9] & 0x7f) +
163 ID3v2_HEADER_SIZE;
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (buf[5] & 0x10)
165 len += ID3v2_HEADER_SIZE;
166 81 return len;
167 }
168
169 static unsigned int get_size(AVIOContext *s, int len)
170 {
171 int v = 0;
172 while (len--)
173 v = (v << 7) + (avio_r8(s) & 0x7F);
174 return v;
175 }
176
177 20 static unsigned int size_to_syncsafe(unsigned int size)
178 {
179 20 return (((size) & (0x7f << 0)) >> 0) +
180 20 (((size) & (0x7f << 8)) >> 1) +
181 40 (((size) & (0x7f << 16)) >> 2) +
182 20 (((size) & (0x7f << 24)) >> 3);
183 }
184
185 /* No real verification, only check that the tag consists of
186 * a combination of capital alpha-numerical characters */
187 4 static int is_tag(const char *buf, unsigned int len)
188 {
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!len)
190 return 0;
191
192
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 while (len--)
193
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if ((buf[len] < 'A' ||
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 buf[len] > 'Z') &&
195 (buf[len] < '0' ||
196 buf[len] > '9'))
197 return 0;
198
199 4 return 1;
200 }
201
202 /**
203 * Return 1 if the tag of length len at the given offset is valid, 0 if not, -1 on error
204 */
205 4 static int check_tag(AVIOContext *s, int offset, unsigned int len)
206 {
207 char tag[4];
208
209
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 if (len > 4 ||
210 4 avio_seek(s, offset, SEEK_SET) < 0 ||
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 avio_read(s, tag, len) < (int)len)
212 return -1;
213
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 else if (!AV_RB32(tag) || is_tag(tag, len))
214 4 return 1;
215
216 return 0;
217 }
218
219 /**
220 * Free GEOB type extra metadata.
221 */
222 2 static void free_geobtag(void *obj)
223 {
224 2 ID3v2ExtraMetaGEOB *geob = obj;
225 2 av_freep(&geob->mime_type);
226 2 av_freep(&geob->file_name);
227 2 av_freep(&geob->description);
228 2 av_freep(&geob->data);
229 2 }
230
231 /**
232 * Decode characters to UTF-8 according to encoding type. The decoded buffer is
233 * always null terminated. Stop reading when either *maxread bytes are read from
234 * pb or U+0000 character is found.
235 *
236 * @param dst Pointer where the address of the buffer with the decoded bytes is
237 * stored. Buffer must be freed by caller.
238 * @param maxread Pointer to maximum number of characters to read from the
239 * AVIOContext. After execution the value is decremented by the number of bytes
240 * actually read.
241 * @returns 0 if no error occurred, dst is uninitialized on error
242 */
243 258 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
244 uint8_t **dst, int *maxread)
245 {
246 int ret;
247 uint8_t tmp;
248 258 uint32_t ch = 1;
249 258 int left = *maxread, dynsize;
250 258 unsigned int (*get)(AVIOContext*) = avio_rb16;
251 AVIOContext *dynbuf;
252
253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 258 times.
258 if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
254 av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
255 return ret;
256 }
257
258
4/5
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 143 times.
✗ Branch 4 not taken.
258 switch (encoding) {
259 89 case ID3v2_ENCODING_ISO8859:
260
4/4
✓ Branch 0 taken 812 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 775 times.
✓ Branch 3 taken 37 times.
864 while (left && ch) {
261 775 ch = avio_r8(pb);
262
4/4
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
776 PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
263 775 left--;
264 }
265 89 break;
266
267 8 case ID3v2_ENCODING_UTF16BOM:
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if ((left -= 2) < 0) {
269 av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
270 ffio_free_dyn_buf(&dynbuf);
271 *dst = NULL;
272 return AVERROR_INVALIDDATA;
273 }
274 8 switch (avio_rb16(pb)) {
275 8 case 0xfffe:
276 8 get = avio_rl16;
277 8 case 0xfeff:
278 8 break;
279 default:
280 av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
281 ffio_free_dyn_buf(&dynbuf);
282 *dst = NULL;
283 *maxread = left;
284 return AVERROR_INVALIDDATA;
285 }
286 // fall-through
287
288 case ID3v2_ENCODING_UTF16BE:
289
4/4
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 197 times.
✓ Branch 3 taken 9 times.
223 while ((left > 1) && ch) {
290
2/10
✓ Branch 0 taken 197 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 197 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
197 GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
291
4/4
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 64 times.
✓ Branch 5 taken 64 times.
✓ Branch 6 taken 64 times.
261 PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
292 }
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (left < 0)
294 left += 2; /* did not read last char from pb */
295 26 break;
296
297 143 case ID3v2_ENCODING_UTF8:
298
4/4
✓ Branch 0 taken 1547 times.
✓ Branch 1 taken 111 times.
✓ Branch 2 taken 1515 times.
✓ Branch 3 taken 32 times.
1658 while (left && ch) {
299 1515 ch = avio_r8(pb);
300 1515 avio_w8(dynbuf, ch);
301 1515 left--;
302 }
303 143 break;
304 default:
305 av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
306 }
307
308
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 236 times.
258 if (ch)
309 22 avio_w8(dynbuf, 0);
310
311 258 dynsize = avio_close_dyn_buf(dynbuf, dst);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258 times.
258 if (dynsize <= 0) {
313 av_freep(dst);
314 return AVERROR(ENOMEM);
315 }
316 258 *maxread = left;
317
318 258 return 0;
319 }
320
321 /**
322 * Parse a text tag.
323 */
324 165 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen,
325 AVDictionary **metadata, const char *key)
326 {
327 uint8_t *dst;
328 165 int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
329 unsigned genre;
330
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (taglen < 1)
332 return;
333
334 165 encoding = avio_r8(pb);
335 165 taglen--; /* account for encoding type byte */
336
337
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 165 times.
165 if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
338 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
339 return;
340 }
341
342
3/4
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 152 times.
165 if (!(strcmp(key, "TCON") && strcmp(key, "TCO")) &&
343
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1) &&
344 genre <= ID3v1_GENRE_MAX) {
345 av_freep(&dst);
346 dst = av_strdup(ff_id3v1_genre_str[genre]);
347
3/4
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 138 times.
165 } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
348 /* dst now contains the key, need to get value */
349 27 key = dst;
350
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
351 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
352 av_freep(&key);
353 return;
354 }
355 27 dict_flags |= AV_DICT_DONT_STRDUP_KEY;
356
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 134 times.
138 } else if (!*dst)
357 4 av_freep(&dst);
358
359
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 4 times.
165 if (dst)
360 161 av_dict_set(metadata, key, dst, dict_flags);
361 }
362
363 1 static void read_uslt(AVFormatContext *s, AVIOContext *pb, int taglen,
364 AVDictionary **metadata)
365 {
366 uint8_t lang[4];
367 1 uint8_t *descriptor = NULL; // 'Content descriptor'
368 uint8_t *text;
369 char *key;
370 int encoding;
371 1 int ok = 0;
372
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (taglen < 4)
374 goto error;
375
376 1 encoding = avio_r8(pb);
377 1 taglen--;
378
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (avio_read(pb, lang, 3) < 3)
380 goto error;
381 1 lang[3] = '\0';
382 1 taglen -= 3;
383
384
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (decode_str(s, pb, encoding, &descriptor, &taglen) < 0 || taglen < 0)
385 goto error;
386
387
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (decode_str(s, pb, encoding, &text, &taglen) < 0 || taglen < 0)
388 goto error;
389
390 // FFmpeg does not support hierarchical metadata, so concatenate the keys.
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 key = av_asprintf("lyrics-%s%s%s", descriptor[0] ? (char *)descriptor : "",
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 descriptor[0] ? "-" : "",
393 lang);
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!key) {
395 av_free(text);
396 goto error;
397 }
398
399 1 av_dict_set(metadata, key, text,
400 AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
401
402 1 ok = 1;
403 1 error:
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ok)
405 av_log(s, AV_LOG_ERROR, "Error reading lyrics, skipped\n");
406 1 av_free(descriptor);
407 1 }
408
409 /**
410 * Parse a comment tag.
411 */
412 7 static void read_comment(AVFormatContext *s, AVIOContext *pb, int taglen,
413 AVDictionary **metadata)
414 {
415 7 const char *key = "comment";
416 uint8_t *dst;
417 7 int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
418 av_unused int language;
419
420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (taglen < 4)
421 return;
422
423 7 encoding = avio_r8(pb);
424 7 language = avio_rl24(pb);
425 7 taglen -= 4;
426
427
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
428 av_log(s, AV_LOG_ERROR, "Error reading comment frame, skipped\n");
429 return;
430 }
431
432
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
7 if (dst && !*dst)
433 2 av_freep(&dst);
434
435
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if (dst) {
436 5 key = (const char *) dst;
437 5 dict_flags |= AV_DICT_DONT_STRDUP_KEY;
438 }
439
440
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
441 av_log(s, AV_LOG_ERROR, "Error reading comment frame, skipped\n");
442 if (dict_flags & AV_DICT_DONT_STRDUP_KEY)
443 av_freep((void*)&key);
444 return;
445 }
446
447
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (dst)
448 7 av_dict_set(metadata, key, (const char *) dst, dict_flags);
449 }
450
451 typedef struct ExtraMetaList {
452 ID3v2ExtraMeta *head, *tail;
453 } ExtraMetaList;
454
455 46 static void list_append(ID3v2ExtraMeta *new_elem, ExtraMetaList *list)
456 {
457
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
46 if (list->tail)
458 23 list->tail->next = new_elem;
459 else
460 23 list->head = new_elem;
461 46 list->tail = new_elem;
462 46 }
463
464 /**
465 * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
466 */
467 2 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen,
468 const char *tag, ExtraMetaList *extra_meta, int isv34)
469 {
470 2 ID3v2ExtraMetaGEOB *geob_data = NULL;
471 2 ID3v2ExtraMeta *new_extra = NULL;
472 char encoding;
473 unsigned int len;
474
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (taglen < 1)
476 return;
477
478 2 new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!new_extra) {
480 av_log(s, AV_LOG_ERROR, "Failed to alloc %"SIZE_SPECIFIER" bytes\n",
481 sizeof(ID3v2ExtraMeta));
482 return;
483 }
484
485 2 geob_data = &new_extra->data.geob;
486
487 /* read encoding type byte */
488 2 encoding = avio_r8(pb);
489 2 taglen--;
490
491 /* read MIME type (always ISO-8859) */
492
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type,
493 2 &taglen) < 0 ||
494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 taglen <= 0)
495 goto fail;
496
497 /* read file name */
498
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0 ||
499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 taglen <= 0)
500 goto fail;
501
502 /* read content description */
503
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0 ||
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 taglen < 0)
505 goto fail;
506
507
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (taglen) {
508 /* save encapsulated binary data */
509 2 geob_data->data = av_malloc(taglen);
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!geob_data->data) {
511 av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
512 goto fail;
513 }
514
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
515 av_log(s, AV_LOG_WARNING,
516 "Error reading GEOB frame, data truncated.\n");
517 2 geob_data->datasize = len;
518 } else {
519 geob_data->data = NULL;
520 geob_data->datasize = 0;
521 }
522
523 /* add data to the list */
524 2 new_extra->tag = "GEOB";
525 2 list_append(new_extra, extra_meta);
526
527 2 return;
528
529 fail:
530 av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
531 free_geobtag(geob_data);
532 av_free(new_extra);
533 return;
534 }
535
536 7 static int is_number(const char *str)
537 {
538
3/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
35 while (*str >= '0' && *str <= '9')
539 28 str++;
540 7 return !*str;
541 }
542
543 8895 static AVDictionaryEntry *get_date_tag(AVDictionary *m, const char *tag)
544 {
545 AVDictionaryEntry *t;
546
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 8888 times.
8895 if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
547
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
7 strlen(t->value) == 4 && is_number(t->value))
548 7 return t;
549 8888 return NULL;
550 }
551
552 4444 static void merge_date(AVDictionary **m)
553 {
554 AVDictionaryEntry *t;
555 4444 char date[17] = { 0 }; // YYYY-MM-DD hh:mm
556
557
3/4
✓ Branch 1 taken 4439 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 4439 times.
✗ Branch 4 not taken.
8883 if (!(t = get_date_tag(*m, "TYER")) &&
558 4439 !(t = get_date_tag(*m, "TYE")))
559 4439 return;
560 5 av_strlcpy(date, t->value, 5);
561 5 av_dict_set(m, "TYER", NULL, 0);
562 5 av_dict_set(m, "TYE", NULL, 0);
563
564
3/4
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
8 if (!(t = get_date_tag(*m, "TDAT")) &&
565 3 !(t = get_date_tag(*m, "TDA")))
566 3 goto finish;
567 2 snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
568 2 av_dict_set(m, "TDAT", NULL, 0);
569 2 av_dict_set(m, "TDA", NULL, 0);
570
571
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
4 if (!(t = get_date_tag(*m, "TIME")) &&
572 2 !(t = get_date_tag(*m, "TIM")))
573 2 goto finish;
574 snprintf(date + 10, sizeof(date) - 10,
575 " %.2s:%.2s", t->value, t->value + 2);
576 av_dict_set(m, "TIME", NULL, 0);
577 av_dict_set(m, "TIM", NULL, 0);
578
579 5 finish:
580
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (date[0])
581 5 av_dict_set(m, "date", date, 0);
582 }
583
584 19 static void free_apic(void *obj)
585 {
586 19 ID3v2ExtraMetaAPIC *apic = obj;
587 19 av_buffer_unref(&apic->buf);
588 19 av_freep(&apic->description);
589 19 }
590
591 19 static void rstrip_spaces(char *buf)
592 {
593 19 size_t len = strlen(buf);
594
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
19 while (len > 0 && buf[len - 1] == ' ')
595 buf[--len] = 0;
596 19 }
597
598 19 static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen,
599 const char *tag, ExtraMetaList *extra_meta, int isv34)
600 {
601 int enc, pic_type;
602 19 char mimetype[64] = {0};
603 19 const CodecMime *mime = ff_id3v2_mime_tags;
604 19 enum AVCodecID id = AV_CODEC_ID_NONE;
605 19 ID3v2ExtraMetaAPIC *apic = NULL;
606 19 ID3v2ExtraMeta *new_extra = NULL;
607 19 int64_t end = avio_tell(pb) + taglen;
608
609
2/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
19 if (taglen <= 4 || (!isv34 && taglen <= 6))
610 goto fail;
611
612 19 new_extra = av_mallocz(sizeof(*new_extra));
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!new_extra)
614 goto fail;
615
616 19 apic = &new_extra->data.apic;
617
618 19 enc = avio_r8(pb);
619 19 taglen--;
620
621 /* mimetype */
622
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (isv34) {
623 19 int ret = avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
624
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 if (ret < 0 || ret >= taglen)
625 goto fail;
626 19 taglen -= ret;
627 } else {
628 if (avio_read(pb, mimetype, 3) < 0)
629 goto fail;
630
631 mimetype[3] = 0;
632 taglen -= 3;
633 }
634
635
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 while (mime->id != AV_CODEC_ID_NONE) {
636
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 44 times.
63 if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
637 19 id = mime->id;
638 19 break;
639 }
640 44 mime++;
641 }
642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (id == AV_CODEC_ID_NONE) {
643 av_log(s, AV_LOG_WARNING,
644 "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
645 goto fail;
646 }
647 19 apic->id = id;
648
649 /* picture type */
650 19 pic_type = avio_r8(pb);
651 19 taglen--;
652
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
653 av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n",
654 pic_type);
655 pic_type = 0;
656 }
657 19 apic->type = ff_id3v2_picture_types[pic_type];
658
659 /* description and picture data */
660
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
661 av_log(s, AV_LOG_ERROR,
662 "Error decoding attached picture description.\n");
663 goto fail;
664 }
665
666 19 apic->buf = av_buffer_alloc(taglen + AV_INPUT_BUFFER_PADDING_SIZE);
667
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 19 times.
19 if (!apic->buf || !taglen || avio_read(pb, apic->buf->data, taglen) != taglen)
668 goto fail;
669 19 memset(apic->buf->data + taglen, 0, AV_INPUT_BUFFER_PADDING_SIZE);
670
671 19 new_extra->tag = "APIC";
672
673 // The description must be unique, and some ID3v2 tag writers add spaces
674 // to write several APIC entries with the same description.
675 19 rstrip_spaces(apic->description);
676 19 list_append(new_extra, extra_meta);
677
678 19 return;
679
680 fail:
681 if (apic)
682 free_apic(apic);
683 av_freep(&new_extra);
684 avio_seek(pb, end, SEEK_SET);
685 }
686
687 10 static void free_chapter(void *obj)
688 {
689 10 ID3v2ExtraMetaCHAP *chap = obj;
690 10 av_freep(&chap->element_id);
691 10 av_dict_free(&chap->meta);
692 10 }
693
694 10 static void read_chapter(AVFormatContext *s, AVIOContext *pb, int len,
695 const char *ttag, ExtraMetaList *extra_meta, int isv34)
696 {
697 int taglen;
698 char tag[5];
699 10 ID3v2ExtraMeta *new_extra = NULL;
700 10 ID3v2ExtraMetaCHAP *chap = NULL;
701
702 10 new_extra = av_mallocz(sizeof(*new_extra));
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!new_extra)
704 10 return;
705
706 10 chap = &new_extra->data.chap;
707
708
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (decode_str(s, pb, 0, &chap->element_id, &len) < 0)
709 goto fail;
710
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (len < 16)
712 goto fail;
713
714 10 chap->start = avio_rb32(pb);
715 10 chap->end = avio_rb32(pb);
716 10 avio_skip(pb, 8);
717
718 10 len -= 16;
719
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 while (len > 10) {
720
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (avio_read(pb, tag, 4) < 4)
721 goto fail;
722 18 tag[4] = 0;
723 18 taglen = avio_rb32(pb);
724 18 avio_skip(pb, 2);
725 18 len -= 10;
726
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (taglen < 0 || taglen > len)
727 goto fail;
728
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (tag[0] == 'T')
729 18 read_ttag(s, pb, taglen, &chap->meta, tag);
730 else
731 avio_skip(pb, taglen);
732 18 len -= taglen;
733 }
734
735 10 ff_metadata_conv(&chap->meta, NULL, ff_id3v2_34_metadata_conv);
736 10 ff_metadata_conv(&chap->meta, NULL, ff_id3v2_4_metadata_conv);
737
738 10 new_extra->tag = "CHAP";
739 10 list_append(new_extra, extra_meta);
740
741 10 return;
742
743 fail:
744 free_chapter(chap);
745 av_freep(&new_extra);
746 }
747
748 15 static void free_priv(void *obj)
749 {
750 15 ID3v2ExtraMetaPRIV *priv = obj;
751 15 av_freep(&priv->owner);
752 15 av_freep(&priv->data);
753 15 }
754
755 15 static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen,
756 const char *tag, ExtraMetaList *extra_meta, int isv34)
757 {
758 ID3v2ExtraMeta *meta;
759 ID3v2ExtraMetaPRIV *priv;
760
761 15 meta = av_mallocz(sizeof(*meta));
762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!meta)
763 15 return;
764
765 15 priv = &meta->data.priv;
766
767
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &priv->owner, &taglen) < 0)
768 goto fail;
769
770 15 priv->data = av_malloc(taglen);
771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!priv->data)
772 goto fail;
773
774 15 priv->datasize = taglen;
775
776
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (avio_read(pb, priv->data, priv->datasize) != priv->datasize)
777 goto fail;
778
779 15 meta->tag = "PRIV";
780 15 list_append(meta, extra_meta);
781
782 15 return;
783
784 fail:
785 free_priv(priv);
786 av_freep(&meta);
787 }
788
789 typedef struct ID3v2EMFunc {
790 const char *tag3;
791 const char *tag4;
792 void (*read)(AVFormatContext *s, AVIOContext *pb, int taglen,
793 const char *tag, ExtraMetaList *extra_meta,
794 int isv34);
795 void (*free)(void *obj);
796 } ID3v2EMFunc;
797
798 static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
799 { "GEO", "GEOB", read_geobtag, free_geobtag },
800 { "PIC", "APIC", read_apic, free_apic },
801 { "CHAP","CHAP", read_chapter, free_chapter },
802 { "PRIV","PRIV", read_priv, free_priv },
803 { NULL }
804 };
805
806 /**
807 * Get the corresponding ID3v2EMFunc struct for a tag.
808 * @param isv34 Determines if v2.2 or v2.3/4 strings are used
809 * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
810 */
811 94 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
812 {
813 94 int i = 0;
814
2/2
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 2 times.
270 while (id3v2_extra_meta_funcs[i].tag3) {
815
5/8
✓ Branch 0 taken 268 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 268 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 268 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 92 times.
✓ Branch 7 taken 176 times.
268 if (tag && !memcmp(tag,
816 (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
817 id3v2_extra_meta_funcs[i].tag3),
818 (isv34 ? 4 : 3)))
819 92 return &id3v2_extra_meta_funcs[i];
820 176 i++;
821 }
822 2 return NULL;
823 }
824
825 37 static void id3v2_parse(AVIOContext *pb, AVDictionary **metadata,
826 AVFormatContext *s, int len, uint8_t version,
827 uint8_t flags, ExtraMetaList *extra_meta)
828 {
829 int isv34, unsync;
830 unsigned tlen;
831 char tag[5];
832 37 int64_t next, end = avio_tell(pb);
833 int taghdrlen;
834 37 const char *reason = NULL;
835 FFIOContext pb_local;
836 AVIOContext *pbx;
837 37 unsigned char *buffer = NULL;
838 37 int buffer_size = 0;
839 37 const ID3v2EMFunc *extra_func = NULL;
840 37 unsigned char *uncompressed_buffer = NULL;
841 37 av_unused int uncompressed_buffer_size = 0;
842 const char *comm_frame;
843
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (end > INT64_MAX - len - 10)
845 return;
846 37 end += len;
847
848 37 av_log(s, AV_LOG_DEBUG, "id3v2 ver:%d flags:%02X len:%d\n", version, flags, len);
849
850
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
37 switch (version) {
851 case 2:
852 if (flags & 0x40) {
853 reason = "compression";
854 goto error;
855 }
856 isv34 = 0;
857 taghdrlen = 6;
858 comm_frame = "COM";
859 break;
860
861 37 case 3:
862 case 4:
863 37 isv34 = 1;
864 37 taghdrlen = 10;
865 37 comm_frame = "COMM";
866 37 break;
867
868 default:
869 reason = "version";
870 goto error;
871 }
872
873 37 unsync = flags & 0x80;
874
875
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
37 if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
876 int extlen = get_size(pb, 4);
877 if (version == 4)
878 /* In v2.4 the length includes the length field we just read. */
879 extlen -= 4;
880
881 if (extlen < 0) {
882 reason = "invalid extended header length";
883 goto error;
884 }
885 avio_skip(pb, extlen);
886 len -= extlen + 4;
887 if (len < 0) {
888 reason = "extended header too long.";
889 goto error;
890 }
891 }
892
893
2/2
✓ Branch 0 taken 1923 times.
✓ Branch 1 taken 37 times.
1960 while (len >= taghdrlen) {
894 1923 unsigned int tflags = 0;
895 1923 int tunsync = 0;
896 1923 int tcomp = 0;
897 1923 int tencr = 0;
898 unsigned long av_unused dlen;
899
900
1/2
✓ Branch 0 taken 1923 times.
✗ Branch 1 not taken.
1923 if (isv34) {
901
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1923 times.
1923 if (avio_read(pb, tag, 4) < 4)
902 break;
903 1923 tag[4] = 0;
904
2/2
✓ Branch 0 taken 977 times.
✓ Branch 1 taken 946 times.
1923 if (version == 3) {
905 977 tlen = avio_rb32(pb);
906 } else {
907 /* some encoders incorrectly uses v3 sizes instead of syncsafe ones
908 * so check the next tag to see which one to use */
909 946 tlen = avio_rb32(pb);
910
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 930 times.
946 if (tlen > 0x7f) {
911
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (tlen < len) {
912 4 int64_t cur = avio_tell(pb);
913
914
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (ffio_ensure_seekback(pb, 2 /* tflags */ + tlen + 4 /* next tag */))
915 break;
916
917
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (check_tag(pb, cur + 2 + size_to_syncsafe(tlen), 4) == 1)
918 4 tlen = size_to_syncsafe(tlen);
919 else if (check_tag(pb, cur + 2 + tlen, 4) != 1)
920 break;
921 4 avio_seek(pb, cur, SEEK_SET);
922 } else
923 12 tlen = size_to_syncsafe(tlen);
924 }
925 }
926 1923 tflags = avio_rb16(pb);
927 1923 tunsync = tflags & ID3v2_FLAG_UNSYNCH;
928 } else {
929 if (avio_read(pb, tag, 3) < 3)
930 break;
931 tag[3] = 0;
932 tlen = avio_rb24(pb);
933 }
934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1923 times.
1923 if (tlen > (1<<28))
935 break;
936 1923 len -= taghdrlen + tlen;
937
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1923 times.
1923 if (len < 0)
939 break;
940
941 1923 next = avio_tell(pb) + tlen;
942
943
2/2
✓ Branch 0 taken 1720 times.
✓ Branch 1 taken 203 times.
1923 if (!tlen) {
944
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1717 times.
1720 if (tag[0])
945 3 av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n",
946 tag);
947 1720 continue;
948 }
949
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
203 if (tflags & ID3v2_FLAG_DATALEN) {
951 if (tlen < 4)
952 break;
953 dlen = avio_rb32(pb);
954 tlen -= 4;
955 } else
956 203 dlen = tlen;
957
958 203 tcomp = tflags & ID3v2_FLAG_COMPRESSION;
959 203 tencr = tflags & ID3v2_FLAG_ENCRYPTION;
960
961 /* skip encrypted tags and, if no zlib, compressed tags */
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
203 if (tencr || (!CONFIG_ZLIB && tcomp)) {
963 const char *type;
964 if (!tcomp)
965 type = "encrypted";
966 else if (!tencr)
967 type = "compressed";
968 else
969 type = "encrypted and compressed";
970
971 av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
972 avio_skip(pb, tlen);
973 /* check for text tag or supported special meta tag */
974
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 147 times.
203 } else if (tag[0] == 'T' ||
975
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 1 times.
56 !memcmp(tag, "USLT", 4) ||
976
3/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
55 !strcmp(tag, comm_frame) ||
977
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 2 times.
48 (extra_meta &&
978 48 (extra_func = get_extra_meta_func(tag, isv34)))) {
979 201 pbx = pb;
980
981
4/6
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 194 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 194 times.
201 if (unsync || tunsync || tcomp) {
982 7 av_fast_malloc(&buffer, &buffer_size, tlen);
983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!buffer) {
984 av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
985 goto seek;
986 }
987 }
988
3/4
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 194 times.
201 if (unsync || tunsync) {
989 7 uint8_t *b = buffer;
990 7 uint8_t *t = buffer;
991 7 uint8_t *end = t + tlen;
992
993
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (avio_read(pb, buffer, tlen) != tlen) {
994 av_log(s, AV_LOG_ERROR, "Failed to read tag data\n");
995 goto seek;
996 }
997
998
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 7 times.
118 while (t != end) {
999 111 *b++ = *t++;
1000
3/6
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
111 if (t != end && t[-1] == 0xff && !t[0])
1001 t++;
1002 }
1003
1004 7 ffio_init_read_context(&pb_local, buffer, b - buffer);
1005 7 tlen = b - buffer;
1006 7 pbx = &pb_local.pub; // read from sync buffer
1007 }
1008
1009 #if CONFIG_ZLIB
1010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 if (tcomp) {
1011 int err;
1012
1013 av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
1014
1015 if (tlen <= 0)
1016 goto seek;
1017 if (dlen / 32768 > tlen)
1018 goto seek;
1019
1020 av_fast_malloc(&uncompressed_buffer, &uncompressed_buffer_size, dlen);
1021 if (!uncompressed_buffer) {
1022 av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
1023 goto seek;
1024 }
1025
1026 if (!(unsync || tunsync)) {
1027 err = avio_read(pb, buffer, tlen);
1028 if (err < 0) {
1029 av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
1030 goto seek;
1031 }
1032 tlen = err;
1033 }
1034
1035 err = uncompress(uncompressed_buffer, &dlen, buffer, tlen);
1036 if (err != Z_OK) {
1037 av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
1038 goto seek;
1039 }
1040 ffio_init_read_context(&pb_local, uncompressed_buffer, dlen);
1041 tlen = dlen;
1042 pbx = &pb_local.pub; // read from sync buffer
1043 }
1044 #endif
1045
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 54 times.
201 if (tag[0] == 'T')
1046 /* parse text tag */
1047 147 read_ttag(s, pbx, tlen, metadata, tag);
1048
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 53 times.
54 else if (!memcmp(tag, "USLT", 4))
1049 1 read_uslt(s, pbx, tlen, metadata);
1050
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 46 times.
53 else if (!strcmp(tag, comm_frame))
1051 7 read_comment(s, pbx, tlen, metadata);
1052 else
1053 /* parse special meta tag */
1054 46 extra_func->read(s, pbx, tlen, tag, extra_meta, isv34);
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (!tag[0]) {
1056 if (tag[1])
1057 av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding\n");
1058 avio_skip(pb, tlen);
1059 break;
1060 }
1061 /* Skip to end of tag */
1062 2 seek:
1063 203 avio_seek(pb, next, SEEK_SET);
1064 }
1065
1066 /* Footer preset, always 10 bytes, skip over it */
1067
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
37 if (version == 4 && flags & 0x10)
1068 end += 10;
1069
1070 37 error:
1071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (reason)
1072 av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n",
1073 version, reason);
1074 37 avio_seek(pb, end, SEEK_SET);
1075 37 av_free(buffer);
1076 37 av_free(uncompressed_buffer);
1077 37 return;
1078 }
1079
1080 4444 static void id3v2_read_internal(AVIOContext *pb, AVDictionary **metadata,
1081 AVFormatContext *s, const char *magic,
1082 ID3v2ExtraMeta **extra_metap, int64_t max_search_size)
1083 {
1084 int len, ret;
1085 uint8_t buf[ID3v2_HEADER_SIZE];
1086 4444 ExtraMetaList extra_meta = { NULL };
1087 int found_header;
1088 int64_t start, off;
1089
1090
1/2
✓ Branch 0 taken 4444 times.
✗ Branch 1 not taken.
4444 if (extra_metap)
1091 4444 *extra_metap = NULL;
1092
1093
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4437 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
4444 if (max_search_size && max_search_size < ID3v2_HEADER_SIZE)
1094 return;
1095
1096 4444 start = avio_tell(pb);
1097 do {
1098 /* save the current offset in case there's nothing to read/skip */
1099 4481 off = avio_tell(pb);
1100
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4467 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
4481 if (max_search_size && off - start >= max_search_size - ID3v2_HEADER_SIZE) {
1101 7 avio_seek(pb, off, SEEK_SET);
1102 7 break;
1103 }
1104
1105 4474 ret = ffio_ensure_seekback(pb, ID3v2_HEADER_SIZE);
1106
1/2
✓ Branch 0 taken 4474 times.
✗ Branch 1 not taken.
4474 if (ret >= 0)
1107 4474 ret = avio_read(pb, buf, ID3v2_HEADER_SIZE);
1108
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4471 times.
4474 if (ret != ID3v2_HEADER_SIZE) {
1109 3 avio_seek(pb, off, SEEK_SET);
1110 3 break;
1111 }
1112 4471 found_header = ff_id3v2_match(buf, magic);
1113
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 4434 times.
4471 if (found_header) {
1114 /* parse ID3v2 header */
1115 37 len = ((buf[6] & 0x7f) << 21) |
1116 37 ((buf[7] & 0x7f) << 14) |
1117 37 ((buf[8] & 0x7f) << 7) |
1118 37 (buf[9] & 0x7f);
1119
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 id3v2_parse(pb, metadata, s, len, buf[3], buf[5],
1120 extra_metap ? &extra_meta : NULL);
1121 } else {
1122 4434 avio_seek(pb, off, SEEK_SET);
1123 }
1124
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 4434 times.
4471 } while (found_header);
1125 4444 ff_metadata_conv(metadata, NULL, ff_id3v2_34_metadata_conv);
1126 4444 ff_metadata_conv(metadata, NULL, id3v2_2_metadata_conv);
1127 4444 ff_metadata_conv(metadata, NULL, ff_id3v2_4_metadata_conv);
1128 4444 merge_date(metadata);
1129
1/2
✓ Branch 0 taken 4444 times.
✗ Branch 1 not taken.
4444 if (extra_metap)
1130 4444 *extra_metap = extra_meta.head;
1131 }
1132
1133 4430 void ff_id3v2_read_dict(AVIOContext *pb, AVDictionary **metadata,
1134 const char *magic, ID3v2ExtraMeta **extra_meta)
1135 {
1136 4430 id3v2_read_internal(pb, metadata, NULL, magic, extra_meta, 0);
1137 4430 }
1138
1139 14 void ff_id3v2_read(AVFormatContext *s, const char *magic,
1140 ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
1141 {
1142 14 id3v2_read_internal(s->pb, &s->metadata, s, magic, extra_meta, max_search_size);
1143 14 }
1144
1145 39 void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
1146 {
1147 39 ID3v2ExtraMeta *current = *extra_meta, *next;
1148 const ID3v2EMFunc *extra_func;
1149
1150
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 39 times.
85 while (current) {
1151
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 if ((extra_func = get_extra_meta_func(current->tag, 1)))
1152 46 extra_func->free(&current->data);
1153 46 next = current->next;
1154 46 av_freep(&current);
1155 46 current = next;
1156 }
1157
1158 39 *extra_meta = NULL;
1159 39 }
1160
1161 17 int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
1162 {
1163 ID3v2ExtraMeta *cur;
1164
1165
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 17 times.
56 for (cur = extra_meta; cur; cur = cur->next) {
1166 ID3v2ExtraMetaAPIC *apic;
1167 AVStream *st;
1168 int ret;
1169
1170
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 19 times.
39 if (strcmp(cur->tag, "APIC"))
1171 20 continue;
1172 19 apic = &cur->data.apic;
1173
1174 19 ret = ff_add_attached_pic(s, NULL, NULL, &apic->buf, 0);
1175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
1176 return ret;
1177 19 st = s->streams[s->nb_streams - 1];
1178 19 st->codecpar->codec_id = apic->id;
1179
1180
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 if (AV_RB64(st->attached_pic.data) == PNGSIG)
1181 4 st->codecpar->codec_id = AV_CODEC_ID_PNG;
1182
1183
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9 times.
19 if (apic->description[0])
1184 10 av_dict_set(&st->metadata, "title", apic->description, 0);
1185
1186 19 av_dict_set(&st->metadata, "comment", apic->type, 0);
1187 }
1188
1189 17 return 0;
1190 }
1191
1192 24 int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *cur)
1193 {
1194 24 AVRational time_base = {1, 1000};
1195 int ret;
1196
1197
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 24 times.
67 for (unsigned i = 0; cur; cur = cur->next) {
1198 ID3v2ExtraMetaCHAP *chap;
1199 AVChapter *chapter;
1200
1201
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 10 times.
43 if (strcmp(cur->tag, "CHAP"))
1202 33 continue;
1203
1204 10 chap = &cur->data.chap;
1205 10 chapter = avpriv_new_chapter(s, i++, time_base, chap->start,
1206 10 chap->end, chap->element_id);
1207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!chapter)
1208 continue;
1209
1210
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if ((ret = av_dict_copy(&chapter->metadata, chap->meta, 0)) < 0)
1211 return ret;
1212 }
1213
1214 24 return 0;
1215 }
1216
1217 13 int ff_id3v2_parse_priv_dict(AVDictionary **metadata, ID3v2ExtraMeta *extra_meta)
1218 {
1219 ID3v2ExtraMeta *cur;
1220 13 int dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL;
1221
1222
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 13 times.
34 for (cur = extra_meta; cur; cur = cur->next) {
1223
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8 times.
21 if (!strcmp(cur->tag, "PRIV")) {
1224 13 ID3v2ExtraMetaPRIV *priv = &cur->data.priv;
1225 AVBPrint bprint;
1226 char *escaped, *key;
1227 int i, ret;
1228
1229
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if ((key = av_asprintf(ID3v2_PRIV_METADATA_PREFIX "%s", priv->owner)) == NULL) {
1230 return AVERROR(ENOMEM);
1231 }
1232
1233 13 av_bprint_init(&bprint, priv->datasize + 1, AV_BPRINT_SIZE_UNLIMITED);
1234
1235
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 13 times.
108 for (i = 0; i < priv->datasize; i++) {
1236
5/6
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 18 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 51 times.
95 if (priv->data[i] < 32 || priv->data[i] > 126 || priv->data[i] == '\\') {
1237 44 av_bprintf(&bprint, "\\x%02x", priv->data[i]);
1238 } else {
1239 51 av_bprint_chars(&bprint, priv->data[i], 1);
1240 }
1241 }
1242
1243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if ((ret = av_bprint_finalize(&bprint, &escaped)) < 0) {
1244 av_free(key);
1245 return ret;
1246 }
1247
1248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if ((ret = av_dict_set(metadata, key, escaped, dict_flags)) < 0) {
1249 return ret;
1250 }
1251 }
1252 }
1253
1254 13 return 0;
1255 }
1256
1257 10 int ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
1258 {
1259 10 return ff_id3v2_parse_priv_dict(&s->metadata, extra_meta);
1260 }
1261