id3v2.c
Go to the documentation of this file.
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 "id3v2.h"
36 #include "id3v1.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/dict.h"
40 #include "avio_internal.h"
41 #include "internal.h"
42 
44  { "TALB", "album"},
45  { "TCOM", "composer"},
46  { "TCON", "genre"},
47  { "TCOP", "copyright"},
48  { "TENC", "encoded_by"},
49  { "TIT2", "title"},
50  { "TLAN", "language"},
51  { "TPE1", "artist"},
52  { "TPE2", "album_artist"},
53  { "TPE3", "performer"},
54  { "TPOS", "disc"},
55  { "TPUB", "publisher"},
56  { "TRCK", "track"},
57  { "TSSE", "encoder"},
58  { 0 }
59 };
60 
62  { "TDRL", "date"},
63  { "TDRC", "date"},
64  { "TDEN", "creation_time"},
65  { "TSOA", "album-sort"},
66  { "TSOP", "artist-sort"},
67  { "TSOT", "title-sort"},
68  { 0 }
69 };
70 
72  { "TAL", "album"},
73  { "TCO", "genre"},
74  { "TT2", "title"},
75  { "TEN", "encoded_by"},
76  { "TP1", "artist"},
77  { "TP2", "album_artist"},
78  { "TP3", "performer"},
79  { "TRK", "track"},
80  { 0 }
81 };
82 
83 
84 const char ff_id3v2_tags[][4] = {
85  "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
86  "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
87  "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
88  "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
89  { 0 },
90 };
91 
92 const char ff_id3v2_4_tags[][4] = {
93  "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
94  "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
95  { 0 },
96 };
97 
98 const char ff_id3v2_3_tags[][4] = {
99  "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
100  { 0 },
101 };
102 
103 const char *ff_id3v2_picture_types[21] = {
104  "Other",
105  "32x32 pixels 'file icon'",
106  "Other file icon",
107  "Cover (front)",
108  "Cover (back)",
109  "Leaflet page",
110  "Media (e.g. label side of CD)",
111  "Lead artist/lead performer/soloist",
112  "Artist/performer",
113  "Conductor",
114  "Band/Orchestra",
115  "Composer",
116  "Lyricist/text writer",
117  "Recording Location",
118  "During recording",
119  "During performance",
120  "Movie/video screen capture",
121  "A bright coloured fish",
122  "Illustration",
123  "Band/artist logotype",
124  "Publisher/Studio logotype",
125 };
126 
128  {"image/gif" , AV_CODEC_ID_GIF},
129  {"image/jpeg", AV_CODEC_ID_MJPEG},
130  {"image/jpg", AV_CODEC_ID_MJPEG},
131  {"image/png" , AV_CODEC_ID_PNG},
132  {"image/tiff", AV_CODEC_ID_TIFF},
133  {"image/bmp", AV_CODEC_ID_BMP},
134  {"JPG", AV_CODEC_ID_MJPEG}, /* ID3v2.2 */
135  {"PNG" , AV_CODEC_ID_PNG}, /* ID3v2.2 */
136  {"", AV_CODEC_ID_NONE},
137 };
138 
139 int ff_id3v2_match(const uint8_t *buf, const char * magic)
140 {
141  return buf[0] == magic[0] &&
142  buf[1] == magic[1] &&
143  buf[2] == magic[2] &&
144  buf[3] != 0xff &&
145  buf[4] != 0xff &&
146  (buf[6] & 0x80) == 0 &&
147  (buf[7] & 0x80) == 0 &&
148  (buf[8] & 0x80) == 0 &&
149  (buf[9] & 0x80) == 0;
150 }
151 
152 int ff_id3v2_tag_len(const uint8_t * buf)
153 {
154  int len = ((buf[6] & 0x7f) << 21) +
155  ((buf[7] & 0x7f) << 14) +
156  ((buf[8] & 0x7f) << 7) +
157  (buf[9] & 0x7f) +
159  if (buf[5] & 0x10)
160  len += ID3v2_HEADER_SIZE;
161  return len;
162 }
163 
164 static unsigned int get_size(AVIOContext *s, int len)
165 {
166  int v = 0;
167  while (len--)
168  v = (v << 7) + (avio_r8(s) & 0x7F);
169  return v;
170 }
171 
172 /**
173  * Free GEOB type extra metadata.
174  */
175 static void free_geobtag(void *obj)
176 {
177  ID3v2ExtraMetaGEOB *geob = obj;
178  av_free(geob->mime_type);
179  av_free(geob->file_name);
180  av_free(geob->description);
181  av_free(geob->data);
182  av_free(geob);
183 }
184 
185 /**
186  * Decode characters to UTF-8 according to encoding type. The decoded buffer is
187  * always null terminated. Stop reading when either *maxread bytes are read from
188  * pb or U+0000 character is found.
189  *
190  * @param dst Pointer where the address of the buffer with the decoded bytes is
191  * stored. Buffer must be freed by caller.
192  * @param maxread Pointer to maximum number of characters to read from the
193  * AVIOContext. After execution the value is decremented by the number of bytes
194  * actually read.
195  * @returns 0 if no error occurred, dst is uninitialized on error
196  */
197 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
198  uint8_t **dst, int *maxread)
199 {
200  int ret;
201  uint8_t tmp;
202  uint32_t ch = 1;
203  int left = *maxread;
204  unsigned int (*get)(AVIOContext*) = avio_rb16;
205  AVIOContext *dynbuf;
206 
207  if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
208  av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
209  return ret;
210  }
211 
212  switch (encoding) {
213 
215  while (left && ch) {
216  ch = avio_r8(pb);
217  PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
218  left--;
219  }
220  break;
221 
223  if ((left -= 2) < 0) {
224  av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
225  avio_close_dyn_buf(dynbuf, dst);
226  av_freep(dst);
227  return AVERROR_INVALIDDATA;
228  }
229  switch (avio_rb16(pb)) {
230  case 0xfffe:
231  get = avio_rl16;
232  case 0xfeff:
233  break;
234  default:
235  av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
236  avio_close_dyn_buf(dynbuf, dst);
237  av_freep(dst);
238  *maxread = left;
239  return AVERROR_INVALIDDATA;
240  }
241  // fall-through
242 
244  while ((left > 1) && ch) {
245  GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
246  PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
247  }
248  if (left < 0)
249  left += 2; /* did not read last char from pb */
250  break;
251 
252  case ID3v2_ENCODING_UTF8:
253  while (left && ch) {
254  ch = avio_r8(pb);
255  avio_w8(dynbuf, ch);
256  left--;
257  }
258  break;
259  default:
260  av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
261  }
262 
263  if (ch)
264  avio_w8(dynbuf, 0);
265 
266  avio_close_dyn_buf(dynbuf, dst);
267  *maxread = left;
268 
269  return 0;
270 }
271 
272 /**
273  * Parse a text tag.
274  */
275 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
276 {
277  uint8_t *dst;
278  int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
279  unsigned genre;
280 
281  if (taglen < 1)
282  return;
283 
284  encoding = avio_r8(pb);
285  taglen--; /* account for encoding type byte */
286 
287  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
288  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
289  return;
290  }
291 
292  if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
293  && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
294  && genre <= ID3v1_GENRE_MAX) {
295  av_freep(&dst);
296  dst = av_strdup(ff_id3v1_genre_str[genre]);
297  } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
298  /* dst now contains the key, need to get value */
299  key = dst;
300  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
301  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
302  av_freep(&key);
303  return;
304  }
305  dict_flags |= AV_DICT_DONT_STRDUP_KEY;
306  } else if (!*dst)
307  av_freep(&dst);
308 
309  if (dst)
310  av_dict_set(&s->metadata, key, dst, dict_flags);
311 }
312 
313 /**
314  * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
315  */
316 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
317 {
318  ID3v2ExtraMetaGEOB *geob_data = NULL;
319  ID3v2ExtraMeta *new_extra = NULL;
320  char encoding;
321  unsigned int len;
322 
323  if (taglen < 1)
324  return;
325 
326  geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
327  if (!geob_data) {
328  av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
329  return;
330  }
331 
332  new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
333  if (!new_extra) {
334  av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
335  goto fail;
336  }
337 
338  /* read encoding type byte */
339  encoding = avio_r8(pb);
340  taglen--;
341 
342  /* read MIME type (always ISO-8859) */
343  if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
344  || taglen <= 0)
345  goto fail;
346 
347  /* read file name */
348  if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
349  || taglen <= 0)
350  goto fail;
351 
352  /* read content description */
353  if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
354  || taglen < 0)
355  goto fail;
356 
357  if (taglen) {
358  /* save encapsulated binary data */
359  geob_data->data = av_malloc(taglen);
360  if (!geob_data->data) {
361  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
362  goto fail;
363  }
364  if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
365  av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
366  geob_data->datasize = len;
367  } else {
368  geob_data->data = NULL;
369  geob_data->datasize = 0;
370  }
371 
372  /* add data to the list */
373  new_extra->tag = "GEOB";
374  new_extra->data = geob_data;
375  new_extra->next = *extra_meta;
376  *extra_meta = new_extra;
377 
378  return;
379 
380 fail:
381  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
382  free_geobtag(geob_data);
383  av_free(new_extra);
384  return;
385 }
386 
387 static int is_number(const char *str)
388 {
389  while (*str >= '0' && *str <= '9') str++;
390  return !*str;
391 }
392 
394 {
396  if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
397  strlen(t->value) == 4 && is_number(t->value))
398  return t;
399  return NULL;
400 }
401 
402 static void merge_date(AVDictionary **m)
403 {
405  char date[17] = {0}; // YYYY-MM-DD hh:mm
406 
407  if (!(t = get_date_tag(*m, "TYER")) &&
408  !(t = get_date_tag(*m, "TYE")))
409  return;
410  av_strlcpy(date, t->value, 5);
411  av_dict_set(m, "TYER", NULL, 0);
412  av_dict_set(m, "TYE", NULL, 0);
413 
414  if (!(t = get_date_tag(*m, "TDAT")) &&
415  !(t = get_date_tag(*m, "TDA")))
416  goto finish;
417  snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
418  av_dict_set(m, "TDAT", NULL, 0);
419  av_dict_set(m, "TDA", NULL, 0);
420 
421  if (!(t = get_date_tag(*m, "TIME")) &&
422  !(t = get_date_tag(*m, "TIM")))
423  goto finish;
424  snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
425  av_dict_set(m, "TIME", NULL, 0);
426  av_dict_set(m, "TIM", NULL, 0);
427 
428 finish:
429  if (date[0])
430  av_dict_set(m, "date", date, 0);
431 }
432 
433 static void free_apic(void *obj)
434 {
435  ID3v2ExtraMetaAPIC *apic = obj;
436  av_buffer_unref(&apic->buf);
437  av_freep(&apic->description);
438  av_freep(&apic);
439 }
440 
441 static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
442 {
443  int enc, pic_type;
444  char mimetype[64];
445  const CodecMime *mime = ff_id3v2_mime_tags;
446  enum AVCodecID id = AV_CODEC_ID_NONE;
447  ID3v2ExtraMetaAPIC *apic = NULL;
448  ID3v2ExtraMeta *new_extra = NULL;
449  int64_t end = avio_tell(pb) + taglen;
450 
451  if (taglen <= 4)
452  goto fail;
453 
454  new_extra = av_mallocz(sizeof(*new_extra));
455  apic = av_mallocz(sizeof(*apic));
456  if (!new_extra || !apic)
457  goto fail;
458 
459  enc = avio_r8(pb);
460  taglen--;
461 
462  /* mimetype */
463  taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
464  while (mime->id != AV_CODEC_ID_NONE) {
465  if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
466  id = mime->id;
467  break;
468  }
469  mime++;
470  }
471  if (id == AV_CODEC_ID_NONE) {
472  av_log(s, AV_LOG_WARNING, "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
473  goto fail;
474  }
475  apic->id = id;
476 
477  /* picture type */
478  pic_type = avio_r8(pb);
479  taglen--;
480  if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
481  av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n", pic_type);
482  pic_type = 0;
483  }
484  apic->type = ff_id3v2_picture_types[pic_type];
485 
486  /* description and picture data */
487  if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
488  av_log(s, AV_LOG_ERROR, "Error decoding attached picture description.\n");
489  goto fail;
490  }
491 
493  if (!apic->buf || !taglen || avio_read(pb, apic->buf->data, taglen) != taglen)
494  goto fail;
495  memset(apic->buf->data + taglen, 0, FF_INPUT_BUFFER_PADDING_SIZE);
496 
497  new_extra->tag = "APIC";
498  new_extra->data = apic;
499  new_extra->next = *extra_meta;
500  *extra_meta = new_extra;
501 
502  return;
503 
504 fail:
505  if (apic)
506  free_apic(apic);
507  av_freep(&new_extra);
508  avio_seek(pb, end, SEEK_SET);
509 }
510 
511 static void read_chapter(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
512 {
513  AVRational time_base = {1, 1000};
514  char title[1024];
515  uint32_t start, end;
516 
517  taglen -= avio_get_str(pb, taglen, title, sizeof(title));
518  if (taglen < 16)
519  return;
520 
521  start = avio_rb32(pb);
522  end = avio_rb32(pb);
523  taglen -= 27;
524  if (taglen > 0) {
525  char tag[4];
526 
527  avio_skip(pb, 8);
528  avio_read(pb, tag, 4);
529  if (!memcmp(tag, "TIT2", 4)) {
530  taglen = FFMIN(taglen, avio_rb32(pb));
531  if (taglen < 0)
532  return;
533  avio_skip(pb, 3);
534  avio_get_str(pb, taglen, title, sizeof(title));
535  }
536  }
537 
538  avpriv_new_chapter(s, s->nb_chapters + 1, time_base, start, end, title);
539 }
540 
541 typedef struct ID3v2EMFunc {
542  const char *tag3;
543  const char *tag4;
545  void (*free)(void *obj);
546 } ID3v2EMFunc;
547 
549  { "GEO", "GEOB", read_geobtag, free_geobtag },
550  { "PIC", "APIC", read_apic, free_apic },
551  { "CHAP","CHAP", read_chapter, NULL },
552  { NULL }
553 };
554 
555 /**
556  * Get the corresponding ID3v2EMFunc struct for a tag.
557  * @param isv34 Determines if v2.2 or v2.3/4 strings are used
558  * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
559  */
560 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
561 {
562  int i = 0;
563  while (id3v2_extra_meta_funcs[i].tag3) {
564  if (tag && !memcmp(tag,
565  (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
566  id3v2_extra_meta_funcs[i].tag3),
567  (isv34 ? 4 : 3)))
568  return &id3v2_extra_meta_funcs[i];
569  i++;
570  }
571  return NULL;
572 }
573 
574 static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
575 {
576  int isv34, unsync;
577  unsigned tlen;
578  char tag[5];
579  int64_t next, end = avio_tell(s->pb) + len;
580  int taghdrlen;
581  const char *reason = NULL;
582  AVIOContext pb;
583  AVIOContext *pbx;
584  unsigned char *buffer = NULL;
585  int buffer_size = 0;
586  const ID3v2EMFunc *extra_func = NULL;
587  unsigned char *uncompressed_buffer = NULL;
588  int uncompressed_buffer_size = 0;
589 
590  av_log(s, AV_LOG_DEBUG, "id3v2 ver:%d flags:%02X len:%d\n", version, flags, len);
591 
592  switch (version) {
593  case 2:
594  if (flags & 0x40) {
595  reason = "compression";
596  goto error;
597  }
598  isv34 = 0;
599  taghdrlen = 6;
600  break;
601 
602  case 3:
603  case 4:
604  isv34 = 1;
605  taghdrlen = 10;
606  break;
607 
608  default:
609  reason = "version";
610  goto error;
611  }
612 
613  unsync = flags & 0x80;
614 
615  if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
616  int extlen = get_size(s->pb, 4);
617  if (version == 4)
618  extlen -= 4; // in v2.4 the length includes the length field we just read
619 
620  if (extlen < 0) {
621  reason = "invalid extended header length";
622  goto error;
623  }
624  avio_skip(s->pb, extlen);
625  len -= extlen + 4;
626  if (len < 0) {
627  reason = "extended header too long.";
628  goto error;
629  }
630  }
631 
632  while (len >= taghdrlen) {
633  unsigned int tflags = 0;
634  int tunsync = 0;
635  int tcomp = 0;
636  int tencr = 0;
637  unsigned long dlen;
638 
639  if (isv34) {
640  avio_read(s->pb, tag, 4);
641  tag[4] = 0;
642  if(version==3){
643  tlen = avio_rb32(s->pb);
644  }else
645  tlen = get_size(s->pb, 4);
646  tflags = avio_rb16(s->pb);
647  tunsync = tflags & ID3v2_FLAG_UNSYNCH;
648  } else {
649  avio_read(s->pb, tag, 3);
650  tag[3] = 0;
651  tlen = avio_rb24(s->pb);
652  }
653  if (tlen > (1<<28))
654  break;
655  len -= taghdrlen + tlen;
656 
657  if (len < 0)
658  break;
659 
660  next = avio_tell(s->pb) + tlen;
661 
662  if (!tlen) {
663  if (tag[0])
664  av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
665  continue;
666  }
667 
668  if (tflags & ID3v2_FLAG_DATALEN) {
669  if (tlen < 4)
670  break;
671  dlen = avio_rb32(s->pb);
672  tlen -= 4;
673  } else
674  dlen = tlen;
675 
676  tcomp = tflags & ID3v2_FLAG_COMPRESSION;
677  tencr = tflags & ID3v2_FLAG_ENCRYPTION;
678 
679  /* skip encrypted tags and, if no zlib, compressed tags */
680  if (tencr || (!CONFIG_ZLIB && tcomp)) {
681  const char *type;
682  if (!tcomp)
683  type = "encrypted";
684  else if (!tencr)
685  type = "compressed";
686  else
687  type = "encrypted and compressed";
688 
689  av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
690  avio_skip(s->pb, tlen);
691  /* check for text tag or supported special meta tag */
692  } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
693  pbx = s->pb;
694 
695  if (unsync || tunsync || tcomp) {
696  av_fast_malloc(&buffer, &buffer_size, tlen);
697  if (!buffer) {
698  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
699  goto seek;
700  }
701  }
702  if (unsync || tunsync) {
703  int64_t end = avio_tell(s->pb) + tlen;
704  uint8_t *b;
705 
706  b = buffer;
707  while (avio_tell(s->pb) < end && b - buffer < tlen) {
708  *b++ = avio_r8(s->pb);
709  if (*(b - 1) == 0xff && avio_tell(s->pb) < end - 1 && b - buffer < tlen) {
710  uint8_t val = avio_r8(s->pb);
711  *b++ = val ? val : avio_r8(s->pb);
712  }
713  }
714  ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL, NULL);
715  tlen = b - buffer;
716  pbx = &pb; // read from sync buffer
717  }
718 
719 #if CONFIG_ZLIB
720  if (tcomp) {
721  int err;
722 
723  av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
724 
725  av_fast_malloc(&uncompressed_buffer, &uncompressed_buffer_size, dlen);
726  if (!uncompressed_buffer) {
727  av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
728  goto seek;
729  }
730 
731  if (!(unsync || tunsync)) {
732  err = avio_read(s->pb, buffer, tlen);
733  if (err < 0) {
734  av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
735  goto seek;
736  }
737  tlen = err;
738  }
739 
740  err = uncompress(uncompressed_buffer, &dlen, buffer, tlen);
741  if (err != Z_OK) {
742  av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
743  goto seek;
744  }
745  ffio_init_context(&pb, uncompressed_buffer, dlen, 0, NULL, NULL, NULL, NULL);
746  tlen = dlen;
747  pbx = &pb; // read from sync buffer
748  }
749 #endif
750  if (tag[0] == 'T')
751  /* parse text tag */
752  read_ttag(s, pbx, tlen, tag);
753  else
754  /* parse special meta tag */
755  extra_func->read(s, pbx, tlen, tag, extra_meta);
756  }
757  else if (!tag[0]) {
758  if (tag[1])
759  av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding\n");
760  avio_skip(s->pb, tlen);
761  break;
762  }
763  /* Skip to end of tag */
764 seek:
765  avio_seek(s->pb, next, SEEK_SET);
766  }
767 
768  if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
769  end += 10;
770 
771  error:
772  if (reason)
773  av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
774  avio_seek(s->pb, end, SEEK_SET);
775  av_free(buffer);
776  av_free(uncompressed_buffer);
777  return;
778 }
779 
780 void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
781 {
782  int len, ret;
783  uint8_t buf[ID3v2_HEADER_SIZE];
784  int found_header;
785  int64_t off;
786 
787  do {
788  /* save the current offset in case there's nothing to read/skip */
789  off = avio_tell(s->pb);
790  ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
791  if (ret != ID3v2_HEADER_SIZE) {
792  avio_seek(s->pb, off, SEEK_SET);
793  break;
794  }
795  found_header = ff_id3v2_match(buf, magic);
796  if (found_header) {
797  /* parse ID3v2 header */
798  len = ((buf[6] & 0x7f) << 21) |
799  ((buf[7] & 0x7f) << 14) |
800  ((buf[8] & 0x7f) << 7) |
801  (buf[9] & 0x7f);
802  ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
803  } else {
804  avio_seek(s->pb, off, SEEK_SET);
805  }
806  } while (found_header);
807  ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
808  ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
809  ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
810  merge_date(&s->metadata);
811 }
812 
814 {
815  ID3v2ExtraMeta *current = *extra_meta, *next;
816  const ID3v2EMFunc *extra_func;
817 
818  while (current) {
819  if ((extra_func = get_extra_meta_func(current->tag, 1)))
820  extra_func->free(current->data);
821  next = current->next;
822  av_freep(&current);
823  current = next;
824  }
825 }
826 
828 {
829  ID3v2ExtraMeta *cur;
830 
831  for (cur = *extra_meta; cur; cur = cur->next) {
832  ID3v2ExtraMetaAPIC *apic;
833  AVStream *st;
834 
835  if (strcmp(cur->tag, "APIC"))
836  continue;
837  apic = cur->data;
838 
839  if (!(st = avformat_new_stream(s, NULL)))
840  return AVERROR(ENOMEM);
841 
844  st->codec->codec_id = apic->id;
845  av_dict_set(&st->metadata, "title", apic->description, 0);
846  av_dict_set(&st->metadata, "comment", apic->type, 0);
847 
849  st->attached_pic.buf = apic->buf;
850  st->attached_pic.data = apic->buf->data;
852  st->attached_pic.stream_index = st->index;
854 
855  apic->buf = NULL;
856  }
857 
858  return 0;
859 }
unsigned int nb_chapters
Definition: avformat.h:1089
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
float v
const char * s
Definition: avisynth_c.h:668
void * data
Definition: id3v2.h:57
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:988
struct ID3v2EMFunc ID3v2EMFunc
enum AVCodecID id
Definition: mxfenc.c:89
static int is_number(const char *str)
Definition: id3v2.c:387
static AVDictionaryEntry * get_date_tag(AVDictionary *m, const char *tag)
Definition: id3v2.c:393
static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
Definition: id3v2.c:574
#define AV_DICT_DONT_OVERWRITE
Don&#39;t overwrite existing entries.
Definition: dict.h:75
static const ID3v2EMFunc id3v2_extra_meta_funcs[]
Definition: id3v2.c:548
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
const char * tag3
Definition: id3v2.c:542
uint32_t datasize
Definition: id3v2.h:62
int index
stream index in AVFormatContext
Definition: avformat.h:644
title('Sinusoid at 1/4 the Spampling Rate')
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
Definition: id3v2.c:441
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:222
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
static void free_apic(void *obj)
Definition: id3v2.c:433
const char ff_id3v2_4_tags[][4]
ID3v2.4-only text information frames.
Definition: id3v2.c:92
#define FF_ARRAY_ELEMS(a)
int version
Definition: avisynth_c.h:666
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
Definition: common.h:349
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:595
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header. ...
Definition: id3v2.c:827
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:976
static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding, uint8_t **dst, int *maxread)
Decode characters to UTF-8 according to encoding type.
Definition: id3v2.c:197
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that&#39;s been allocated with av_malloc() and children.
Definition: dict.h:69
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
void(* read)(AVFormatContext *, AVIOContext *, int, char *, ID3v2ExtraMeta **)
Definition: id3v2.c:544
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Format I/O context.
Definition: avformat.h:944
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Public dictionary API.
#define ID3v2_HEADER_SIZE
Definition: id3v2.h:30
const AVMetadataConv ff_id3v2_34_metadata_conv[]
Definition: id3v2.c:43
window constants for m
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
#define b
Definition: input.c:42
end end
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * description
Definition: id3v2.h:65
uint8_t * data
const char * type
Definition: id3v2.h:71
uint32_t tag
Definition: movenc.c:894
enum AVCodecID id
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
uint8_t * data
Definition: id3v2.h:66
void(* free)(void *obj)
Definition: id3v2.c:545
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define GET_UTF16(val, GET_16BIT, ERROR)
Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:321
static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
Definition: id3v2.c:316
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_DICT_MATCH_CASE
Definition: dict.h:67
AVDictionary * metadata
Definition: avformat.h:1092
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
const AVMetadataConv ff_id3v2_4_metadata_conv[]
Definition: id3v2.c:61
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:813
void av_log(void *avcl, int level, const char *fmt,...)
Send the specified message to the log if the level is less than or equal to the current av_log_level...
Definition: log.c:246
#define CONFIG_ZLIB
Definition: config.h:351
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:82
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:103
int flags
A combination of AV_PKT_FLAG values.
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:127
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:603
#define FFMIN(a, b)
Definition: common.h:58
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and chilren.
Definition: dict.h:72
ret
Definition: avfilter.c:821
static void free_geobtag(void *obj)
Free GEOB type extra metadata.
Definition: id3v2.c:175
static unsigned int get_size(AVIOContext *s, int len)
Definition: id3v2.c:164
t
Definition: genspecsines3.m:6
#define ID3v2_FLAG_UNSYNCH
Definition: id3v2.h:38
AVDictionary * metadata
Definition: avformat.h:711
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:627
static void merge_date(AVDictionary **m)
Definition: id3v2.c:402
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag, including supported extra metadata.
Definition: id3v2.c:780
Stream structure.
Definition: avformat.h:643
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:220
const char ff_id3v2_tags[][4]
A list of text information frames allowed in both ID3 v2.3 and v2.4 http://www.id3.org/id3v2.4.0-frames http://www.id3.org/id3v2.4.0-changes.
Definition: id3v2.c:84
NULL
Definition: eval.c:55
enum AVMediaType codec_type
static void read_chapter(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
Definition: id3v2.c:511
struct ID3v2ExtraMeta * next
Definition: id3v2.h:58
typedef void(RENAME(mix_any_func_type))
enum AVCodecID codec_id
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
AVIOContext * pb
I/O context.
Definition: avformat.h:977
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
uint8_t * data
The data buffer.
Definition: buffer.h:89
#define ID3v2_FLAG_COMPRESSION
Definition: id3v2.h:40
enum AVCodecID id
Definition: id3v2.h:73
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
const char *const ff_id3v1_genre_str[147+1]
ID3v1 genres.
Definition: id3v1.c:27
uint8_t * description
Definition: id3v2.h:72
void * buf
Definition: avisynth_c.h:594
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:62
synthesis window for stochastic i
rational number numerator/denominator
Definition: rational.h:43
static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
Parse a text tag.
Definition: id3v2.c:275
#define snprintf
Definition: snprintf.h:34
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
int size
Size of data in bytes.
Definition: buffer.h:93
static int flags
Definition: cpu.c:23
static const ID3v2EMFunc * get_extra_meta_func(const char *tag, int isv34)
Get the corresponding ID3v2EMFunc struct for a tag.
Definition: id3v2.c:560
uint8_t * mime_type
Definition: id3v2.h:63
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition: id3v2.c:139
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:71
const char * tag
Definition: id3v2.h:56
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:700
#define ID3v2_FLAG_ENCRYPTION
Definition: id3v2.h:39
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
const char * tag4
Definition: id3v2.c:543
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
const char ff_id3v2_3_tags[][4]
ID3v2.3-only text information frames.
Definition: id3v2.c:98
uint8_t * file_name
Definition: id3v2.h:64
char * value
Definition: dict.h:82
int len
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition: id3v2.c:152
AVBufferRef * buf
Definition: id3v2.h:70
or the Software in violation of any applicable export control laws in any jurisdiction Except as provided by mandatorily applicable UPF has no obligation to provide you with source code to the Software In the event Software contains any source unless expressly licensed for other it is provided solely for reference purposes pursuant to the terms of this License Source code may not be redistributed unless expressly provided for in this License INTELLECTUAL AND INDUSTRIAL PROPERTY RIGHTS Title to Software and all associated intellectual and industrial property rights is retained by UPF and or its licensors UPF warrants that the copyright in the Software is owned by it or licensed to it and that it has the power and authority to grant the Licence You shall keep intact all patent or trademark notices and all notices that refer to the Software and the Licence and to the disclaimer of warranties DISCLAIMER OF WARRANTY This Software is a work in progress It is not a finished work and may therefore contain defects or bugs inherent to this type of software development For this reason
#define AV_LOG_INFO
Definition: log.h:156
void INT64 start
Definition: avisynth_c.h:594
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:633
#define ID3v2_FLAG_DATALEN
Definition: id3v2.h:37
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:725
static const AVMetadataConv id3v2_2_metadata_conv[]
Definition: id3v2.c:71