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