FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/avstring.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/intreadwrite.h"
38 #include "avio_internal.h"
39 #include "internal.h"
40 #include "id3v1.h"
41 #include "id3v2.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  { "USLT", "lyrics" },
59  { 0 }
60 };
61 
63  { "TCMP", "compilation" },
64  { "TDRC", "date" },
65  { "TDRL", "date" },
66  { "TDEN", "creation_time" },
67  { "TSOA", "album-sort" },
68  { "TSOP", "artist-sort" },
69  { "TSOT", "title-sort" },
70  { 0 }
71 };
72 
74  { "TAL", "album" },
75  { "TCO", "genre" },
76  { "TCP", "compilation" },
77  { "TT2", "title" },
78  { "TEN", "encoded_by" },
79  { "TP1", "artist" },
80  { "TP2", "album_artist" },
81  { "TP3", "performer" },
82  { "TRK", "track" },
83  { 0 }
84 };
85 
86 const char ff_id3v2_tags[][4] = {
87  "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
88  "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
89  "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
90  "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
91  { 0 },
92 };
93 
94 const char ff_id3v2_4_tags[][4] = {
95  "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
96  "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
97  { 0 },
98 };
99 
100 const char ff_id3v2_3_tags[][4] = {
101  "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
102  { 0 },
103 };
104 
105 const char *ff_id3v2_picture_types[21] = {
106  "Other",
107  "32x32 pixels 'file icon'",
108  "Other file icon",
109  "Cover (front)",
110  "Cover (back)",
111  "Leaflet page",
112  "Media (e.g. label side of CD)",
113  "Lead artist/lead performer/soloist",
114  "Artist/performer",
115  "Conductor",
116  "Band/Orchestra",
117  "Composer",
118  "Lyricist/text writer",
119  "Recording Location",
120  "During recording",
121  "During performance",
122  "Movie/video screen capture",
123  "A bright coloured fish",
124  "Illustration",
125  "Band/artist logotype",
126  "Publisher/Studio logotype",
127 };
128 
130  { "image/gif", AV_CODEC_ID_GIF },
131  { "image/jpeg", AV_CODEC_ID_MJPEG },
132  { "image/jpg", AV_CODEC_ID_MJPEG },
133  { "image/png", AV_CODEC_ID_PNG },
134  { "image/tiff", AV_CODEC_ID_TIFF },
135  { "image/bmp", AV_CODEC_ID_BMP },
136  { "JPG", AV_CODEC_ID_MJPEG }, /* ID3v2.2 */
137  { "PNG", AV_CODEC_ID_PNG }, /* ID3v2.2 */
138  { "", AV_CODEC_ID_NONE },
139 };
140 
141 int ff_id3v2_match(const uint8_t *buf, const char *magic)
142 {
143  return buf[0] == magic[0] &&
144  buf[1] == magic[1] &&
145  buf[2] == magic[2] &&
146  buf[3] != 0xff &&
147  buf[4] != 0xff &&
148  (buf[6] & 0x80) == 0 &&
149  (buf[7] & 0x80) == 0 &&
150  (buf[8] & 0x80) == 0 &&
151  (buf[9] & 0x80) == 0;
152 }
153 
155 {
156  int len = ((buf[6] & 0x7f) << 21) +
157  ((buf[7] & 0x7f) << 14) +
158  ((buf[8] & 0x7f) << 7) +
159  (buf[9] & 0x7f) +
161  if (buf[5] & 0x10)
162  len += ID3v2_HEADER_SIZE;
163  return len;
164 }
165 
166 static unsigned int get_size(AVIOContext *s, int len)
167 {
168  int v = 0;
169  while (len--)
170  v = (v << 7) + (avio_r8(s) & 0x7F);
171  return v;
172 }
173 
174 static unsigned int size_to_syncsafe(unsigned int size)
175 {
176  return (((size) & (0x7f << 0)) >> 0) +
177  (((size) & (0x7f << 8)) >> 1) +
178  (((size) & (0x7f << 16)) >> 2) +
179  (((size) & (0x7f << 24)) >> 3);
180 }
181 
182 /* No real verification, only check that the tag consists of
183  * a combination of capital alpha-numerical characters */
184 static int is_tag(const char *buf, unsigned int len)
185 {
186  if (!len)
187  return 0;
188 
189  while (len--)
190  if ((buf[len] < 'A' ||
191  buf[len] > 'Z') &&
192  (buf[len] < '0' ||
193  buf[len] > '9'))
194  return 0;
195 
196  return 1;
197 }
198 
199 /**
200  * Return 1 if the tag of length len at the given offset is valid, 0 if not, -1 on error
201  */
202 static int check_tag(AVIOContext *s, int offset, unsigned int len)
203 {
204  char tag[4];
205 
206  if (len > 4 ||
207  avio_seek(s, offset, SEEK_SET) < 0 ||
208  avio_read(s, tag, len) < (int)len)
209  return -1;
210  else if (!AV_RB32(tag) || is_tag(tag, len))
211  return 1;
212 
213  return 0;
214 }
215 
216 /**
217  * Free GEOB type extra metadata.
218  */
219 static void free_geobtag(void *obj)
220 {
221  ID3v2ExtraMetaGEOB *geob = obj;
222  av_freep(&geob->mime_type);
223  av_freep(&geob->file_name);
224  av_freep(&geob->description);
225  av_freep(&geob->data);
226  av_free(geob);
227 }
228 
229 /**
230  * Decode characters to UTF-8 according to encoding type. The decoded buffer is
231  * always null terminated. Stop reading when either *maxread bytes are read from
232  * pb or U+0000 character is found.
233  *
234  * @param dst Pointer where the address of the buffer with the decoded bytes is
235  * stored. Buffer must be freed by caller.
236  * @param maxread Pointer to maximum number of characters to read from the
237  * AVIOContext. After execution the value is decremented by the number of bytes
238  * actually read.
239  * @returns 0 if no error occurred, dst is uninitialized on error
240  */
241 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
242  uint8_t **dst, int *maxread)
243 {
244  int ret;
245  uint8_t tmp;
246  uint32_t ch = 1;
247  int left = *maxread;
248  unsigned int (*get)(AVIOContext*) = avio_rb16;
249  AVIOContext *dynbuf;
250 
251  if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
252  av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
253  return ret;
254  }
255 
256  switch (encoding) {
258  while (left && ch) {
259  ch = avio_r8(pb);
260  PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
261  left--;
262  }
263  break;
264 
266  if ((left -= 2) < 0) {
267  av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
268  ffio_free_dyn_buf(&dynbuf);
269  *dst = NULL;
270  return AVERROR_INVALIDDATA;
271  }
272  switch (avio_rb16(pb)) {
273  case 0xfffe:
274  get = avio_rl16;
275  case 0xfeff:
276  break;
277  default:
278  av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
279  ffio_free_dyn_buf(&dynbuf);
280  *dst = NULL;
281  *maxread = left;
282  return AVERROR_INVALIDDATA;
283  }
284  // fall-through
285 
287  while ((left > 1) && ch) {
288  GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
289  PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
290  }
291  if (left < 0)
292  left += 2; /* did not read last char from pb */
293  break;
294 
295  case ID3v2_ENCODING_UTF8:
296  while (left && ch) {
297  ch = avio_r8(pb);
298  avio_w8(dynbuf, ch);
299  left--;
300  }
301  break;
302  default:
303  av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
304  }
305 
306  if (ch)
307  avio_w8(dynbuf, 0);
308 
309  avio_close_dyn_buf(dynbuf, dst);
310  *maxread = left;
311 
312  return 0;
313 }
314 
315 /**
316  * Parse a text tag.
317  */
318 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen,
319  AVDictionary **metadata, const char *key)
320 {
321  uint8_t *dst;
322  int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
323  unsigned genre;
324 
325  if (taglen < 1)
326  return;
327 
328  encoding = avio_r8(pb);
329  taglen--; /* account for encoding type byte */
330 
331  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
332  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
333  return;
334  }
335 
336  if (!(strcmp(key, "TCON") && strcmp(key, "TCO")) &&
337  (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1) &&
338  genre <= ID3v1_GENRE_MAX) {
339  av_freep(&dst);
340  dst = av_strdup(ff_id3v1_genre_str[genre]);
341  } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
342  /* dst now contains the key, need to get value */
343  key = dst;
344  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
345  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
346  av_freep(&key);
347  return;
348  }
349  dict_flags |= AV_DICT_DONT_STRDUP_KEY;
350  } else if (!*dst)
351  av_freep(&dst);
352 
353  if (dst)
354  av_dict_set(metadata, key, dst, dict_flags);
355 }
356 
357 static void read_uslt(AVFormatContext *s, AVIOContext *pb, int taglen,
358  AVDictionary **metadata)
359 {
360  uint8_t lang[4];
361  uint8_t *descriptor = NULL; // 'Content descriptor'
362  uint8_t *text = NULL;
363  char *key = NULL;
364  int encoding;
365  int ok = 0;
366 
367  if (taglen < 1)
368  goto error;
369 
370  encoding = avio_r8(pb);
371  taglen--;
372 
373  if (avio_read(pb, lang, 3) < 3)
374  goto error;
375  lang[3] = '\0';
376  taglen -= 3;
377 
378  if (decode_str(s, pb, encoding, &descriptor, &taglen) < 0)
379  goto error;
380 
381  if (decode_str(s, pb, encoding, &text, &taglen) < 0)
382  goto error;
383 
384  // FFmpeg does not support hierarchical metadata, so concatenate the keys.
385  key = av_asprintf("lyrics-%s%s%s", descriptor[0] ? (char *)descriptor : "",
386  descriptor[0] ? "-" : "",
387  lang);
388  if (!key)
389  goto error;
390 
391  av_dict_set(metadata, key, text, 0);
392 
393  ok = 1;
394 error:
395  if (!ok)
396  av_log(s, AV_LOG_ERROR, "Error reading lyrics, skipped\n");
397  av_free(descriptor);
398  av_free(text);
399  av_free(key);
400 }
401 
402 /**
403  * Parse a comment tag.
404  */
405 static void read_comment(AVFormatContext *s, AVIOContext *pb, int taglen,
406  AVDictionary **metadata)
407 {
408  const char *key = "comment";
409  uint8_t *dst;
410  int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
411  av_unused int language;
412 
413  if (taglen < 4)
414  return;
415 
416  encoding = avio_r8(pb);
417  language = avio_rl24(pb);
418  taglen -= 4;
419 
420  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
421  av_log(s, AV_LOG_ERROR, "Error reading comment frame, skipped\n");
422  return;
423  }
424 
425  if (dst && !*dst)
426  av_freep(&dst);
427 
428  if (dst) {
429  key = (const char *) dst;
430  dict_flags |= AV_DICT_DONT_STRDUP_KEY;
431  }
432 
433  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
434  av_log(s, AV_LOG_ERROR, "Error reading comment frame, skipped\n");
435  if (dict_flags & AV_DICT_DONT_STRDUP_KEY)
436  av_freep((void*)&key);
437  return;
438  }
439 
440  if (dst)
441  av_dict_set(metadata, key, (const char *) dst, dict_flags);
442 }
443 
444 /**
445  * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
446  */
447 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen,
448  const char *tag, ID3v2ExtraMeta **extra_meta,
449  int isv34)
450 {
451  ID3v2ExtraMetaGEOB *geob_data = NULL;
452  ID3v2ExtraMeta *new_extra = NULL;
453  char encoding;
454  unsigned int len;
455 
456  if (taglen < 1)
457  return;
458 
459  geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
460  if (!geob_data) {
461  av_log(s, AV_LOG_ERROR, "Failed to alloc %"SIZE_SPECIFIER" bytes\n",
462  sizeof(ID3v2ExtraMetaGEOB));
463  return;
464  }
465 
466  new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
467  if (!new_extra) {
468  av_log(s, AV_LOG_ERROR, "Failed to alloc %"SIZE_SPECIFIER" bytes\n",
469  sizeof(ID3v2ExtraMeta));
470  goto fail;
471  }
472 
473  /* read encoding type byte */
474  encoding = avio_r8(pb);
475  taglen--;
476 
477  /* read MIME type (always ISO-8859) */
478  if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type,
479  &taglen) < 0 ||
480  taglen <= 0)
481  goto fail;
482 
483  /* read file name */
484  if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0 ||
485  taglen <= 0)
486  goto fail;
487 
488  /* read content description */
489  if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0 ||
490  taglen < 0)
491  goto fail;
492 
493  if (taglen) {
494  /* save encapsulated binary data */
495  geob_data->data = av_malloc(taglen);
496  if (!geob_data->data) {
497  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
498  goto fail;
499  }
500  if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
502  "Error reading GEOB frame, data truncated.\n");
503  geob_data->datasize = len;
504  } else {
505  geob_data->data = NULL;
506  geob_data->datasize = 0;
507  }
508 
509  /* add data to the list */
510  new_extra->tag = "GEOB";
511  new_extra->data = geob_data;
512  new_extra->next = *extra_meta;
513  *extra_meta = new_extra;
514 
515  return;
516 
517 fail:
518  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
519  free_geobtag(geob_data);
520  av_free(new_extra);
521  return;
522 }
523 
524 static int is_number(const char *str)
525 {
526  while (*str >= '0' && *str <= '9')
527  str++;
528  return !*str;
529 }
530 
532 {
534  if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
535  strlen(t->value) == 4 && is_number(t->value))
536  return t;
537  return NULL;
538 }
539 
540 static void merge_date(AVDictionary **m)
541 {
543  char date[17] = { 0 }; // YYYY-MM-DD hh:mm
544 
545  if (!(t = get_date_tag(*m, "TYER")) &&
546  !(t = get_date_tag(*m, "TYE")))
547  return;
548  av_strlcpy(date, t->value, 5);
549  av_dict_set(m, "TYER", NULL, 0);
550  av_dict_set(m, "TYE", NULL, 0);
551 
552  if (!(t = get_date_tag(*m, "TDAT")) &&
553  !(t = get_date_tag(*m, "TDA")))
554  goto finish;
555  snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
556  av_dict_set(m, "TDAT", NULL, 0);
557  av_dict_set(m, "TDA", NULL, 0);
558 
559  if (!(t = get_date_tag(*m, "TIME")) &&
560  !(t = get_date_tag(*m, "TIM")))
561  goto finish;
562  snprintf(date + 10, sizeof(date) - 10,
563  " %.2s:%.2s", t->value, t->value + 2);
564  av_dict_set(m, "TIME", NULL, 0);
565  av_dict_set(m, "TIM", NULL, 0);
566 
567 finish:
568  if (date[0])
569  av_dict_set(m, "date", date, 0);
570 }
571 
572 static void free_apic(void *obj)
573 {
574  ID3v2ExtraMetaAPIC *apic = obj;
575  av_buffer_unref(&apic->buf);
576  av_freep(&apic->description);
577  av_freep(&apic);
578 }
579 
580 static void rstrip_spaces(char *buf)
581 {
582  size_t len = strlen(buf);
583  while (len > 0 && buf[len - 1] == ' ')
584  buf[--len] = 0;
585 }
586 
587 static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen,
588  const char *tag, ID3v2ExtraMeta **extra_meta,
589  int isv34)
590 {
591  int enc, pic_type;
592  char mimetype[64];
593  const CodecMime *mime = ff_id3v2_mime_tags;
594  enum AVCodecID id = AV_CODEC_ID_NONE;
595  ID3v2ExtraMetaAPIC *apic = NULL;
596  ID3v2ExtraMeta *new_extra = NULL;
597  int64_t end = avio_tell(pb) + taglen;
598 
599  if (taglen <= 4 || (!isv34 && taglen <= 6))
600  goto fail;
601 
602  new_extra = av_mallocz(sizeof(*new_extra));
603  apic = av_mallocz(sizeof(*apic));
604  if (!new_extra || !apic)
605  goto fail;
606 
607  enc = avio_r8(pb);
608  taglen--;
609 
610  /* mimetype */
611  if (isv34) {
612  taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
613  } else {
614  avio_read(pb, mimetype, 3);
615  mimetype[3] = 0;
616  taglen -= 3;
617  }
618 
619  while (mime->id != AV_CODEC_ID_NONE) {
620  if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
621  id = mime->id;
622  break;
623  }
624  mime++;
625  }
626  if (id == AV_CODEC_ID_NONE) {
628  "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
629  goto fail;
630  }
631  apic->id = id;
632 
633  /* picture type */
634  pic_type = avio_r8(pb);
635  taglen--;
636  if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
637  av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n",
638  pic_type);
639  pic_type = 0;
640  }
641  apic->type = ff_id3v2_picture_types[pic_type];
642 
643  /* description and picture data */
644  if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
645  av_log(s, AV_LOG_ERROR,
646  "Error decoding attached picture description.\n");
647  goto fail;
648  }
649 
651  if (!apic->buf || !taglen || avio_read(pb, apic->buf->data, taglen) != taglen)
652  goto fail;
653  memset(apic->buf->data + taglen, 0, AV_INPUT_BUFFER_PADDING_SIZE);
654 
655  new_extra->tag = "APIC";
656  new_extra->data = apic;
657  new_extra->next = *extra_meta;
658  *extra_meta = new_extra;
659 
660  // The description must be unique, and some ID3v2 tag writers add spaces
661  // to write several APIC entries with the same description.
662  rstrip_spaces(apic->description);
663 
664  return;
665 
666 fail:
667  if (apic)
668  free_apic(apic);
669  av_freep(&new_extra);
670  avio_seek(pb, end, SEEK_SET);
671 }
672 
673 static void read_chapter(AVFormatContext *s, AVIOContext *pb, int len, const char *ttag, ID3v2ExtraMeta **extra_meta, int isv34)
674 {
675  AVRational time_base = {1, 1000};
676  uint32_t start, end;
677  AVChapter *chapter;
678  uint8_t *dst = NULL;
679  int taglen;
680  char tag[5];
681 
682  if (!s) {
683  /* We should probably just put the chapter data to extra_meta here
684  * and do the AVFormatContext-needing part in a separate
685  * ff_id3v2_parse_apic()-like function. */
686  av_log(NULL, AV_LOG_DEBUG, "No AVFormatContext, skipped ID3 chapter data\n");
687  return;
688  }
689 
690  if (decode_str(s, pb, 0, &dst, &len) < 0)
691  return;
692  if (len < 16)
693  return;
694 
695  start = avio_rb32(pb);
696  end = avio_rb32(pb);
697  avio_skip(pb, 8);
698 
699  chapter = avpriv_new_chapter(s, s->nb_chapters + 1, time_base, start, end, dst);
700  if (!chapter) {
701  av_free(dst);
702  return;
703  }
704 
705  len -= 16;
706  while (len > 10) {
707  if (avio_read(pb, tag, 4) < 4)
708  goto end;
709  tag[4] = 0;
710  taglen = avio_rb32(pb);
711  avio_skip(pb, 2);
712  len -= 10;
713  if (taglen < 0 || taglen > len)
714  goto end;
715  if (tag[0] == 'T')
716  read_ttag(s, pb, taglen, &chapter->metadata, tag);
717  else
718  avio_skip(pb, taglen);
719  len -= taglen;
720  }
721 
722  ff_metadata_conv(&chapter->metadata, NULL, ff_id3v2_34_metadata_conv);
723  ff_metadata_conv(&chapter->metadata, NULL, ff_id3v2_4_metadata_conv);
724 end:
725  av_free(dst);
726 }
727 
728 static void free_priv(void *obj)
729 {
730  ID3v2ExtraMetaPRIV *priv = obj;
731  av_freep(&priv->owner);
732  av_freep(&priv->data);
733  av_freep(&priv);
734 }
735 
736 static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen,
737  const char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
738 {
739  ID3v2ExtraMeta *meta;
740  ID3v2ExtraMetaPRIV *priv;
741 
742  meta = av_mallocz(sizeof(*meta));
743  priv = av_mallocz(sizeof(*priv));
744 
745  if (!meta || !priv)
746  goto fail;
747 
748  if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &priv->owner, &taglen) < 0)
749  goto fail;
750 
751  priv->data = av_malloc(taglen);
752  if (!priv->data)
753  goto fail;
754 
755  priv->datasize = taglen;
756 
757  if (avio_read(pb, priv->data, priv->datasize) != priv->datasize)
758  goto fail;
759 
760  meta->tag = "PRIV";
761  meta->data = priv;
762  meta->next = *extra_meta;
763  *extra_meta = meta;
764 
765  return;
766 
767 fail:
768  if (priv)
769  free_priv(priv);
770  av_freep(&meta);
771 }
772 
773 typedef struct ID3v2EMFunc {
774  const char *tag3;
775  const char *tag4;
776  void (*read)(AVFormatContext *s, AVIOContext *pb, int taglen,
777  const char *tag, ID3v2ExtraMeta **extra_meta,
778  int isv34);
779  void (*free)(void *obj);
780 } ID3v2EMFunc;
781 
783  { "GEO", "GEOB", read_geobtag, free_geobtag },
784  { "PIC", "APIC", read_apic, free_apic },
785  { "CHAP","CHAP", read_chapter, NULL },
786  { "PRIV","PRIV", read_priv, free_priv },
787  { NULL }
788 };
789 
790 /**
791  * Get the corresponding ID3v2EMFunc struct for a tag.
792  * @param isv34 Determines if v2.2 or v2.3/4 strings are used
793  * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
794  */
795 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
796 {
797  int i = 0;
798  while (id3v2_extra_meta_funcs[i].tag3) {
799  if (tag && !memcmp(tag,
800  (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
801  id3v2_extra_meta_funcs[i].tag3),
802  (isv34 ? 4 : 3)))
803  return &id3v2_extra_meta_funcs[i];
804  i++;
805  }
806  return NULL;
807 }
808 
809 static void id3v2_parse(AVIOContext *pb, AVDictionary **metadata,
811  uint8_t flags, ID3v2ExtraMeta **extra_meta)
812 {
813  int isv34, unsync;
814  unsigned tlen;
815  char tag[5];
816  int64_t next, end = avio_tell(pb) + len;
817  int taghdrlen;
818  const char *reason = NULL;
819  AVIOContext pb_local;
820  AVIOContext *pbx;
821  unsigned char *buffer = NULL;
822  int buffer_size = 0;
823  const ID3v2EMFunc *extra_func = NULL;
824  unsigned char *uncompressed_buffer = NULL;
825  av_unused int uncompressed_buffer_size = 0;
826 
827  av_log(s, AV_LOG_DEBUG, "id3v2 ver:%d flags:%02X len:%d\n", version, flags, len);
828 
829  switch (version) {
830  case 2:
831  if (flags & 0x40) {
832  reason = "compression";
833  goto error;
834  }
835  isv34 = 0;
836  taghdrlen = 6;
837  break;
838 
839  case 3:
840  case 4:
841  isv34 = 1;
842  taghdrlen = 10;
843  break;
844 
845  default:
846  reason = "version";
847  goto error;
848  }
849 
850  unsync = flags & 0x80;
851 
852  if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
853  int extlen = get_size(pb, 4);
854  if (version == 4)
855  /* In v2.4 the length includes the length field we just read. */
856  extlen -= 4;
857 
858  if (extlen < 0) {
859  reason = "invalid extended header length";
860  goto error;
861  }
862  avio_skip(pb, extlen);
863  len -= extlen + 4;
864  if (len < 0) {
865  reason = "extended header too long.";
866  goto error;
867  }
868  }
869 
870  while (len >= taghdrlen) {
871  unsigned int tflags = 0;
872  int tunsync = 0;
873  int tcomp = 0;
874  int tencr = 0;
875  unsigned long av_unused dlen;
876 
877  if (isv34) {
878  if (avio_read(pb, tag, 4) < 4)
879  break;
880  tag[4] = 0;
881  if (version == 3) {
882  tlen = avio_rb32(pb);
883  } else {
884  /* some encoders incorrectly uses v3 sizes instead of syncsafe ones
885  * so check the next tag to see which one to use */
886  tlen = avio_rb32(pb);
887  if (tlen > 0x7f) {
888  if (tlen < len) {
889  int64_t cur = avio_tell(pb);
890 
891  if (ffio_ensure_seekback(pb, 2 /* tflags */ + tlen + 4 /* next tag */))
892  break;
893 
894  if (check_tag(pb, cur + 2 + size_to_syncsafe(tlen), 4) == 1)
895  tlen = size_to_syncsafe(tlen);
896  else if (check_tag(pb, cur + 2 + tlen, 4) != 1)
897  break;
898  avio_seek(pb, cur, SEEK_SET);
899  } else
900  tlen = size_to_syncsafe(tlen);
901  }
902  }
903  tflags = avio_rb16(pb);
904  tunsync = tflags & ID3v2_FLAG_UNSYNCH;
905  } else {
906  if (avio_read(pb, tag, 3) < 3)
907  break;
908  tag[3] = 0;
909  tlen = avio_rb24(pb);
910  }
911  if (tlen > (1<<28))
912  break;
913  len -= taghdrlen + tlen;
914 
915  if (len < 0)
916  break;
917 
918  next = avio_tell(pb) + tlen;
919 
920  if (!tlen) {
921  if (tag[0])
922  av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n",
923  tag);
924  continue;
925  }
926 
927  if (tflags & ID3v2_FLAG_DATALEN) {
928  if (tlen < 4)
929  break;
930  dlen = avio_rb32(pb);
931  tlen -= 4;
932  } else
933  dlen = tlen;
934 
935  tcomp = tflags & ID3v2_FLAG_COMPRESSION;
936  tencr = tflags & ID3v2_FLAG_ENCRYPTION;
937 
938  /* skip encrypted tags and, if no zlib, compressed tags */
939  if (tencr || (!CONFIG_ZLIB && tcomp)) {
940  const char *type;
941  if (!tcomp)
942  type = "encrypted";
943  else if (!tencr)
944  type = "compressed";
945  else
946  type = "encrypted and compressed";
947 
948  av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
949  avio_skip(pb, tlen);
950  /* check for text tag or supported special meta tag */
951  } else if (tag[0] == 'T' ||
952  !memcmp(tag, "USLT", 4) ||
953  !memcmp(tag, "COMM", 4) ||
954  (extra_meta &&
955  (extra_func = get_extra_meta_func(tag, isv34)))) {
956  pbx = pb;
957 
958  if (unsync || tunsync || tcomp) {
959  av_fast_malloc(&buffer, &buffer_size, tlen);
960  if (!buffer) {
961  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
962  goto seek;
963  }
964  }
965  if (unsync || tunsync) {
966  int64_t end = avio_tell(pb) + tlen;
967  uint8_t *b;
968 
969  b = buffer;
970  while (avio_tell(pb) < end && b - buffer < tlen && !pb->eof_reached) {
971  *b++ = avio_r8(pb);
972  if (*(b - 1) == 0xff && avio_tell(pb) < end - 1 &&
973  b - buffer < tlen &&
974  !pb->eof_reached ) {
975  uint8_t val = avio_r8(pb);
976  *b++ = val ? val : avio_r8(pb);
977  }
978  }
979  ffio_init_context(&pb_local, buffer, b - buffer, 0, NULL, NULL, NULL,
980  NULL);
981  tlen = b - buffer;
982  pbx = &pb_local; // read from sync buffer
983  }
984 
985 #if CONFIG_ZLIB
986  if (tcomp) {
987  int err;
988 
989  av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
990 
991  av_fast_malloc(&uncompressed_buffer, &uncompressed_buffer_size, dlen);
992  if (!uncompressed_buffer) {
993  av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
994  goto seek;
995  }
996 
997  if (!(unsync || tunsync)) {
998  err = avio_read(pb, buffer, tlen);
999  if (err < 0) {
1000  av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
1001  goto seek;
1002  }
1003  tlen = err;
1004  }
1005 
1006  err = uncompress(uncompressed_buffer, &dlen, buffer, tlen);
1007  if (err != Z_OK) {
1008  av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
1009  goto seek;
1010  }
1011  ffio_init_context(&pb_local, uncompressed_buffer, dlen, 0, NULL, NULL, NULL, NULL);
1012  tlen = dlen;
1013  pbx = &pb_local; // read from sync buffer
1014  }
1015 #endif
1016  if (tag[0] == 'T')
1017  /* parse text tag */
1018  read_ttag(s, pbx, tlen, metadata, tag);
1019  else if (!memcmp(tag, "USLT", 4))
1020  read_uslt(s, pbx, tlen, metadata);
1021  else if (!memcmp(tag, "COMM", 4))
1022  read_comment(s, pbx, tlen, metadata);
1023  else
1024  /* parse special meta tag */
1025  extra_func->read(s, pbx, tlen, tag, extra_meta, isv34);
1026  } else if (!tag[0]) {
1027  if (tag[1])
1028  av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding\n");
1029  avio_skip(pb, tlen);
1030  break;
1031  }
1032  /* Skip to end of tag */
1033 seek:
1034  avio_seek(pb, next, SEEK_SET);
1035  }
1036 
1037  /* Footer preset, always 10 bytes, skip over it */
1038  if (version == 4 && flags & 0x10)
1039  end += 10;
1040 
1041 error:
1042  if (reason)
1043  av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n",
1044  version, reason);
1045  avio_seek(pb, end, SEEK_SET);
1046  av_free(buffer);
1047  av_free(uncompressed_buffer);
1048  return;
1049 }
1050 
1051 static void id3v2_read_internal(AVIOContext *pb, AVDictionary **metadata,
1052  AVFormatContext *s, const char *magic,
1053  ID3v2ExtraMeta **extra_meta, int64_t max_search_size)
1054 {
1055  int len, ret;
1057  int found_header;
1058  int64_t start, off;
1059 
1060  if (max_search_size && max_search_size < ID3v2_HEADER_SIZE)
1061  return;
1062 
1063  start = avio_tell(pb);
1064  do {
1065  /* save the current offset in case there's nothing to read/skip */
1066  off = avio_tell(pb);
1067  if (max_search_size && off - start >= max_search_size - ID3v2_HEADER_SIZE) {
1068  avio_seek(pb, off, SEEK_SET);
1069  break;
1070  }
1071 
1072  ret = avio_read(pb, buf, ID3v2_HEADER_SIZE);
1073  if (ret != ID3v2_HEADER_SIZE) {
1074  avio_seek(pb, off, SEEK_SET);
1075  break;
1076  }
1077  found_header = ff_id3v2_match(buf, magic);
1078  if (found_header) {
1079  /* parse ID3v2 header */
1080  len = ((buf[6] & 0x7f) << 21) |
1081  ((buf[7] & 0x7f) << 14) |
1082  ((buf[8] & 0x7f) << 7) |
1083  (buf[9] & 0x7f);
1084  id3v2_parse(pb, metadata, s, len, buf[3], buf[5], extra_meta);
1085  } else {
1086  avio_seek(pb, off, SEEK_SET);
1087  }
1088  } while (found_header);
1089  ff_metadata_conv(metadata, NULL, ff_id3v2_34_metadata_conv);
1090  ff_metadata_conv(metadata, NULL, id3v2_2_metadata_conv);
1091  ff_metadata_conv(metadata, NULL, ff_id3v2_4_metadata_conv);
1092  merge_date(metadata);
1093 }
1094 
1096  const char *magic, ID3v2ExtraMeta **extra_meta)
1097 {
1098  id3v2_read_internal(pb, metadata, NULL, magic, extra_meta, 0);
1099 }
1100 
1101 void ff_id3v2_read(AVFormatContext *s, const char *magic,
1102  ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
1103 {
1104  id3v2_read_internal(s->pb, &s->metadata, s, magic, extra_meta, max_search_size);
1105 }
1106 
1108 {
1109  ID3v2ExtraMeta *current = *extra_meta, *next;
1110  const ID3v2EMFunc *extra_func;
1111 
1112  while (current) {
1113  if ((extra_func = get_extra_meta_func(current->tag, 1)))
1114  extra_func->free(current->data);
1115  next = current->next;
1116  av_freep(&current);
1117  current = next;
1118  }
1119 
1120  *extra_meta = NULL;
1121 }
1122 
1124 {
1125  ID3v2ExtraMeta *cur;
1126 
1127  for (cur = *extra_meta; cur; cur = cur->next) {
1128  ID3v2ExtraMetaAPIC *apic;
1129  AVStream *st;
1130 
1131  if (strcmp(cur->tag, "APIC"))
1132  continue;
1133  apic = cur->data;
1134 
1135  if (!(st = avformat_new_stream(s, NULL)))
1136  return AVERROR(ENOMEM);
1137 
1140  st->codecpar->codec_id = apic->id;
1141 
1142  if (AV_RB64(apic->buf->data) == 0x89504e470d0a1a0a)
1144 
1145  if (apic->description[0])
1146  av_dict_set(&st->metadata, "title", apic->description, 0);
1147 
1148  av_dict_set(&st->metadata, "comment", apic->type, 0);
1149 
1151  st->attached_pic.buf = apic->buf;
1152  st->attached_pic.data = apic->buf->data;
1154  st->attached_pic.stream_index = st->index;
1156 
1157  apic->buf = NULL;
1158  }
1159 
1160  return 0;
1161 }
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1543
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
const char * s
Definition: avisynth_c.h:768
void * data
Definition: id3v2.h:57
Bytestream IO Context.
Definition: avio.h:147
#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...
Definition: buffer.c:124
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1280
enum AVCodecID id
Definition: mxfenc.c:104
static int is_number(const char *str)
Definition: id3v2.c:524
static AVDictionaryEntry * get_date_tag(AVDictionary *m, const char *tag)
Definition: id3v2.c:531
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:79
static const ID3v2EMFunc id3v2_extra_meta_funcs[]
Definition: id3v2.c:782
static void id3v2_parse(AVIOContext *pb, AVDictionary **metadata, AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
Definition: id3v2.c:809
const char * tag3
Definition: id3v2.c:774
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3980
uint32_t datasize
Definition: id3v2.h:62
int index
stream index in AVFormatContext
Definition: avformat.h:890
int size
Definition: avcodec.h:1602
const char * b
Definition: vf_curves.c:113
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:223
static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
Definition: id3v2.c:587
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:304
static void free_apic(void *obj)
Definition: id3v2.c:572
const char ff_id3v2_4_tags[][4]
ID3v2.4-only text information frames.
Definition: id3v2.c:94
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
#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:414
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:742
AVDictionary * metadata
Definition: avformat.h:1299
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:1123
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1268
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:241
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function...
Definition: dict.h:73
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:4317
Format I/O context.
Definition: avformat.h:1338
char str[32]
Definition: internal.h:50
void(* read)(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
Definition: id3v2.c:776
Public dictionary API.
static void id3v2_read_internal(AVIOContext *pb, AVDictionary **metadata, AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta, int64_t max_search_size)
Definition: id3v2.c:1051
#define ID3v2_HEADER_SIZE
Definition: id3v2.h:30
const AVMetadataConv ff_id3v2_34_metadata_conv[]
Definition: id3v2.c:43
uint8_t
#define av_malloc(s)
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:757
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4193
static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
Definition: id3v2.c:736
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
uint8_t * description
Definition: id3v2.h:65
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
static void finish(void)
Definition: movenc.c:344
uint8_t * data
Definition: avcodec.h:1601
const char * type
Definition: id3v2.h:71
uint32_t tag
Definition: movenc.c:1382
enum AVCodecID id
Definition: internal.h:51
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:604
uint8_t * data
Definition: id3v2.h:66
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1633
static int is_tag(const char *buf, unsigned int len)
Definition: id3v2.c:184
#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:386
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:191
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
Definition: dict.h:69
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1554
const AVMetadataConv ff_id3v2_4_metadata_conv[]
Definition: id3v2.c:62
#define AVERROR(e)
Definition: error.h:43
static void free_priv(void *obj)
Definition: id3v2.c:728
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3976
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1584
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1107
static void read_comment(AVFormatContext *s, AVIOContext *pb, int taglen, AVDictionary **metadata)
Parse a comment tag.
Definition: id3v2.c:405
uint8_t * data
Definition: id3v2.h:78
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
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:83
#define fail()
Definition: checkasm.h:83
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:105
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1607
static unsigned int size_to_syncsafe(unsigned int size)
Definition: id3v2.c:174
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:595
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:499
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:129
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:750
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, AVDictionary **metadata, const char *key)
Parse a text tag.
Definition: id3v2.c:318
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static void free_geobtag(void *obj)
Free GEOB type extra metadata.
Definition: id3v2.c:219
static unsigned int get_size(AVIOContext *s, int len)
Definition: id3v2.c:166
static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
Definition: id3v2.c:447
#define ID3v2_FLAG_UNSYNCH
Definition: id3v2.h:38
AVDictionary * metadata
Definition: avformat.h:958
uint8_t * owner
Definition: id3v2.h:77
#define FF_ARRAY_ELEMS(a)
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:859
static void merge_date(AVDictionary **m)
Definition: id3v2.c:540
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1308
Stream structure.
Definition: avformat.h:889
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:86
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:267
struct ID3v2ExtraMeta * next
Definition: id3v2.h:58
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:182
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
uint8_t * description
Definition: id3v2.h:72
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
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:70
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define snprintf
Definition: snprintf.h:34
int size
Size of data in bytes.
Definition: buffer.h:93
static void read_uslt(AVFormatContext *s, AVIOContext *pb, int taglen, AVDictionary **metadata)
Definition: id3v2.c:357
#define SIZE_SPECIFIER
Definition: internal.h:252
static int flags
Definition: cpu.c:47
static int check_tag(AVIOContext *s, int offset, unsigned int len)
Return 1 if the tag of length len at the given offset is valid, 0 if not, -1 on error.
Definition: id3v2.c:202
static void rstrip_spaces(char *buf)
Definition: id3v2.c:580
static const ID3v2EMFunc * get_extra_meta_func(const char *tag, int isv34)
Get the corresponding ID3v2EMFunc struct for a tag.
Definition: id3v2.c:795
uint8_t * mime_type
Definition: id3v2.h:63
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:930
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:710
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:87
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition: id3v2.c:141
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:81
const char * tag
Definition: id3v2.h:56
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:947
#define ID3v2_FLAG_ENCRYPTION
Definition: id3v2.h:39
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
const char * tag4
Definition: id3v2.c:775
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
Read an ID3v2 tag, including supported extra metadata and chapters.
Definition: id3v2.c:1101
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_YASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:734
const char ff_id3v2_3_tags[][4]
ID3v2.3-only text information frames.
Definition: id3v2.c:100
uint8_t * file_name
Definition: id3v2.h:64
#define av_free(p)
void ff_id3v2_read_dict(AVIOContext *pb, AVDictionary **metadata, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag into specified dictionary and retrieve supported extra metadata.
Definition: id3v2.c:1095
char * value
Definition: dict.h:87
int eof_reached
true if eof reached
Definition: avio.h:222
int len
static uint8_t tmp[8]
Definition: des.c:38
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition: id3v2.c:154
AVBufferRef * buf
Definition: id3v2.h:70
uint32_t datasize
Definition: id3v2.h:79
void(* free)(void *obj)
Definition: id3v2.c:779
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
AVCodecParameters * codecpar
Definition: avformat.h:1241
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:782
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:718
#define ID3v2_FLAG_DATALEN
Definition: id3v2.h:37
int stream_index
Definition: avcodec.h:1603
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:27
static void read_chapter(AVFormatContext *s, AVIOContext *pb, int len, const char *ttag, ID3v2ExtraMeta **extra_meta, int isv34)
Definition: id3v2.c:673
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:976
GLuint buffer
Definition: opengl_enc.c:102
#define av_unused
Definition: attributes.h:126
static const AVMetadataConv id3v2_2_metadata_conv[]
Definition: id3v2.c:73