Coverage Report

Created: 2026-07-30 07:17

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