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