FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/id3v2.c
Date: 2026-05-03 08:24:11
Exec Total Coverage
Lines: 477 669 71.3%
Functions: 32 33 97.0%
Branches: 269 458 58.7%

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