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