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 * const 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 free_chapter(void *obj)
674 {
675  ID3v2ExtraMetaCHAP *chap = obj;
676  av_freep(&chap->element_id);
677  av_dict_free(&chap->meta);
678  av_freep(&chap);
679 }
680 
681 static void read_chapter(AVFormatContext *s, AVIOContext *pb, int len, const char *ttag, ID3v2ExtraMeta **extra_meta, int isv34)
682 {
683  int taglen;
684  char tag[5];
685  ID3v2ExtraMeta *new_extra = NULL;
686  ID3v2ExtraMetaCHAP *chap = NULL;
687 
688  new_extra = av_mallocz(sizeof(*new_extra));
689  chap = av_mallocz(sizeof(*chap));
690 
691  if (!new_extra || !chap)
692  goto fail;
693 
694  if (decode_str(s, pb, 0, &chap->element_id, &len) < 0)
695  goto fail;
696 
697  if (len < 16)
698  goto fail;
699 
700  chap->start = avio_rb32(pb);
701  chap->end = avio_rb32(pb);
702  avio_skip(pb, 8);
703 
704  len -= 16;
705  while (len > 10) {
706  if (avio_read(pb, tag, 4) < 4)
707  goto fail;
708  tag[4] = 0;
709  taglen = avio_rb32(pb);
710  avio_skip(pb, 2);
711  len -= 10;
712  if (taglen < 0 || taglen > len)
713  goto fail;
714  if (tag[0] == 'T')
715  read_ttag(s, pb, taglen, &chap->meta, tag);
716  else
717  avio_skip(pb, taglen);
718  len -= taglen;
719  }
720 
721  ff_metadata_conv(&chap->meta, NULL, ff_id3v2_34_metadata_conv);
722  ff_metadata_conv(&chap->meta, NULL, ff_id3v2_4_metadata_conv);
723 
724  new_extra->tag = "CHAP";
725  new_extra->data = chap;
726  new_extra->next = *extra_meta;
727  *extra_meta = new_extra;
728 
729  return;
730 
731 fail:
732  if (chap)
733  free_chapter(chap);
734  av_freep(&new_extra);
735 }
736 
737 static void free_priv(void *obj)
738 {
739  ID3v2ExtraMetaPRIV *priv = obj;
740  av_freep(&priv->owner);
741  av_freep(&priv->data);
742  av_freep(&priv);
743 }
744 
745 static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen,
746  const char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
747 {
748  ID3v2ExtraMeta *meta;
749  ID3v2ExtraMetaPRIV *priv;
750 
751  meta = av_mallocz(sizeof(*meta));
752  priv = av_mallocz(sizeof(*priv));
753 
754  if (!meta || !priv)
755  goto fail;
756 
757  if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &priv->owner, &taglen) < 0)
758  goto fail;
759 
760  priv->data = av_malloc(taglen);
761  if (!priv->data)
762  goto fail;
763 
764  priv->datasize = taglen;
765 
766  if (avio_read(pb, priv->data, priv->datasize) != priv->datasize)
767  goto fail;
768 
769  meta->tag = "PRIV";
770  meta->data = priv;
771  meta->next = *extra_meta;
772  *extra_meta = meta;
773 
774  return;
775 
776 fail:
777  if (priv)
778  free_priv(priv);
779  av_freep(&meta);
780 }
781 
782 typedef struct ID3v2EMFunc {
783  const char *tag3;
784  const char *tag4;
785  void (*read)(AVFormatContext *s, AVIOContext *pb, int taglen,
786  const char *tag, ID3v2ExtraMeta **extra_meta,
787  int isv34);
788  void (*free)(void *obj);
789 } ID3v2EMFunc;
790 
792  { "GEO", "GEOB", read_geobtag, free_geobtag },
793  { "PIC", "APIC", read_apic, free_apic },
794  { "CHAP","CHAP", read_chapter, free_chapter },
795  { "PRIV","PRIV", read_priv, free_priv },
796  { NULL }
797 };
798 
799 /**
800  * Get the corresponding ID3v2EMFunc struct for a tag.
801  * @param isv34 Determines if v2.2 or v2.3/4 strings are used
802  * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
803  */
804 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
805 {
806  int i = 0;
807  while (id3v2_extra_meta_funcs[i].tag3) {
808  if (tag && !memcmp(tag,
809  (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
810  id3v2_extra_meta_funcs[i].tag3),
811  (isv34 ? 4 : 3)))
812  return &id3v2_extra_meta_funcs[i];
813  i++;
814  }
815  return NULL;
816 }
817 
818 static void id3v2_parse(AVIOContext *pb, AVDictionary **metadata,
820  uint8_t flags, ID3v2ExtraMeta **extra_meta)
821 {
822  int isv34, unsync;
823  unsigned tlen;
824  char tag[5];
825  int64_t next, end = avio_tell(pb) + len;
826  int taghdrlen;
827  const char *reason = NULL;
828  AVIOContext pb_local;
829  AVIOContext *pbx;
830  unsigned char *buffer = NULL;
831  int buffer_size = 0;
832  const ID3v2EMFunc *extra_func = NULL;
833  unsigned char *uncompressed_buffer = NULL;
834  av_unused int uncompressed_buffer_size = 0;
835  const char *comm_frame;
836 
837  av_log(s, AV_LOG_DEBUG, "id3v2 ver:%d flags:%02X len:%d\n", version, flags, len);
838 
839  switch (version) {
840  case 2:
841  if (flags & 0x40) {
842  reason = "compression";
843  goto error;
844  }
845  isv34 = 0;
846  taghdrlen = 6;
847  comm_frame = "COM";
848  break;
849 
850  case 3:
851  case 4:
852  isv34 = 1;
853  taghdrlen = 10;
854  comm_frame = "COMM";
855  break;
856 
857  default:
858  reason = "version";
859  goto error;
860  }
861 
862  unsync = flags & 0x80;
863 
864  if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
865  int extlen = get_size(pb, 4);
866  if (version == 4)
867  /* In v2.4 the length includes the length field we just read. */
868  extlen -= 4;
869 
870  if (extlen < 0) {
871  reason = "invalid extended header length";
872  goto error;
873  }
874  avio_skip(pb, extlen);
875  len -= extlen + 4;
876  if (len < 0) {
877  reason = "extended header too long.";
878  goto error;
879  }
880  }
881 
882  while (len >= taghdrlen) {
883  unsigned int tflags = 0;
884  int tunsync = 0;
885  int tcomp = 0;
886  int tencr = 0;
887  unsigned long av_unused dlen;
888 
889  if (isv34) {
890  if (avio_read(pb, tag, 4) < 4)
891  break;
892  tag[4] = 0;
893  if (version == 3) {
894  tlen = avio_rb32(pb);
895  } else {
896  /* some encoders incorrectly uses v3 sizes instead of syncsafe ones
897  * so check the next tag to see which one to use */
898  tlen = avio_rb32(pb);
899  if (tlen > 0x7f) {
900  if (tlen < len) {
901  int64_t cur = avio_tell(pb);
902 
903  if (ffio_ensure_seekback(pb, 2 /* tflags */ + tlen + 4 /* next tag */))
904  break;
905 
906  if (check_tag(pb, cur + 2 + size_to_syncsafe(tlen), 4) == 1)
907  tlen = size_to_syncsafe(tlen);
908  else if (check_tag(pb, cur + 2 + tlen, 4) != 1)
909  break;
910  avio_seek(pb, cur, SEEK_SET);
911  } else
912  tlen = size_to_syncsafe(tlen);
913  }
914  }
915  tflags = avio_rb16(pb);
916  tunsync = tflags & ID3v2_FLAG_UNSYNCH;
917  } else {
918  if (avio_read(pb, tag, 3) < 3)
919  break;
920  tag[3] = 0;
921  tlen = avio_rb24(pb);
922  }
923  if (tlen > (1<<28))
924  break;
925  len -= taghdrlen + tlen;
926 
927  if (len < 0)
928  break;
929 
930  next = avio_tell(pb) + tlen;
931 
932  if (!tlen) {
933  if (tag[0])
934  av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n",
935  tag);
936  continue;
937  }
938 
939  if (tflags & ID3v2_FLAG_DATALEN) {
940  if (tlen < 4)
941  break;
942  dlen = avio_rb32(pb);
943  tlen -= 4;
944  } else
945  dlen = tlen;
946 
947  tcomp = tflags & ID3v2_FLAG_COMPRESSION;
948  tencr = tflags & ID3v2_FLAG_ENCRYPTION;
949 
950  /* skip encrypted tags and, if no zlib, compressed tags */
951  if (tencr || (!CONFIG_ZLIB && tcomp)) {
952  const char *type;
953  if (!tcomp)
954  type = "encrypted";
955  else if (!tencr)
956  type = "compressed";
957  else
958  type = "encrypted and compressed";
959 
960  av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
961  avio_skip(pb, tlen);
962  /* check for text tag or supported special meta tag */
963  } else if (tag[0] == 'T' ||
964  !memcmp(tag, "USLT", 4) ||
965  !strcmp(tag, comm_frame) ||
966  (extra_meta &&
967  (extra_func = get_extra_meta_func(tag, isv34)))) {
968  pbx = pb;
969 
970  if (unsync || tunsync || tcomp) {
971  av_fast_malloc(&buffer, &buffer_size, tlen);
972  if (!buffer) {
973  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
974  goto seek;
975  }
976  }
977  if (unsync || tunsync) {
978  int64_t end = avio_tell(pb) + tlen;
979  uint8_t *b;
980 
981  b = buffer;
982  while (avio_tell(pb) < end && b - buffer < tlen && !pb->eof_reached) {
983  *b++ = avio_r8(pb);
984  if (*(b - 1) == 0xff && avio_tell(pb) < end - 1 &&
985  b - buffer < tlen &&
986  !pb->eof_reached ) {
987  uint8_t val = avio_r8(pb);
988  *b++ = val ? val : avio_r8(pb);
989  }
990  }
991  ffio_init_context(&pb_local, buffer, b - buffer, 0, NULL, NULL, NULL,
992  NULL);
993  tlen = b - buffer;
994  pbx = &pb_local; // read from sync buffer
995  }
996 
997 #if CONFIG_ZLIB
998  if (tcomp) {
999  int err;
1000 
1001  av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
1002 
1003  av_fast_malloc(&uncompressed_buffer, &uncompressed_buffer_size, dlen);
1004  if (!uncompressed_buffer) {
1005  av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
1006  goto seek;
1007  }
1008 
1009  if (!(unsync || tunsync)) {
1010  err = avio_read(pb, buffer, tlen);
1011  if (err < 0) {
1012  av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
1013  goto seek;
1014  }
1015  tlen = err;
1016  }
1017 
1018  err = uncompress(uncompressed_buffer, &dlen, buffer, tlen);
1019  if (err != Z_OK) {
1020  av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
1021  goto seek;
1022  }
1023  ffio_init_context(&pb_local, uncompressed_buffer, dlen, 0, NULL, NULL, NULL, NULL);
1024  tlen = dlen;
1025  pbx = &pb_local; // read from sync buffer
1026  }
1027 #endif
1028  if (tag[0] == 'T')
1029  /* parse text tag */
1030  read_ttag(s, pbx, tlen, metadata, tag);
1031  else if (!memcmp(tag, "USLT", 4))
1032  read_uslt(s, pbx, tlen, metadata);
1033  else if (!strcmp(tag, comm_frame))
1034  read_comment(s, pbx, tlen, metadata);
1035  else
1036  /* parse special meta tag */
1037  extra_func->read(s, pbx, tlen, tag, extra_meta, isv34);
1038  } else if (!tag[0]) {
1039  if (tag[1])
1040  av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding\n");
1041  avio_skip(pb, tlen);
1042  break;
1043  }
1044  /* Skip to end of tag */
1045 seek:
1046  avio_seek(pb, next, SEEK_SET);
1047  }
1048 
1049  /* Footer preset, always 10 bytes, skip over it */
1050  if (version == 4 && flags & 0x10)
1051  end += 10;
1052 
1053 error:
1054  if (reason)
1055  av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n",
1056  version, reason);
1057  avio_seek(pb, end, SEEK_SET);
1058  av_free(buffer);
1059  av_free(uncompressed_buffer);
1060  return;
1061 }
1062 
1063 static void id3v2_read_internal(AVIOContext *pb, AVDictionary **metadata,
1064  AVFormatContext *s, const char *magic,
1065  ID3v2ExtraMeta **extra_meta, int64_t max_search_size)
1066 {
1067  int len, ret;
1069  int found_header;
1070  int64_t start, off;
1071 
1072  if (max_search_size && max_search_size < ID3v2_HEADER_SIZE)
1073  return;
1074 
1075  start = avio_tell(pb);
1076  do {
1077  /* save the current offset in case there's nothing to read/skip */
1078  off = avio_tell(pb);
1079  if (max_search_size && off - start >= max_search_size - ID3v2_HEADER_SIZE) {
1080  avio_seek(pb, off, SEEK_SET);
1081  break;
1082  }
1083 
1085  if (ret >= 0)
1086  ret = avio_read(pb, buf, ID3v2_HEADER_SIZE);
1087  if (ret != ID3v2_HEADER_SIZE) {
1088  avio_seek(pb, off, SEEK_SET);
1089  break;
1090  }
1091  found_header = ff_id3v2_match(buf, magic);
1092  if (found_header) {
1093  /* parse ID3v2 header */
1094  len = ((buf[6] & 0x7f) << 21) |
1095  ((buf[7] & 0x7f) << 14) |
1096  ((buf[8] & 0x7f) << 7) |
1097  (buf[9] & 0x7f);
1098  id3v2_parse(pb, metadata, s, len, buf[3], buf[5], extra_meta);
1099  } else {
1100  avio_seek(pb, off, SEEK_SET);
1101  }
1102  } while (found_header);
1103  ff_metadata_conv(metadata, NULL, ff_id3v2_34_metadata_conv);
1104  ff_metadata_conv(metadata, NULL, id3v2_2_metadata_conv);
1105  ff_metadata_conv(metadata, NULL, ff_id3v2_4_metadata_conv);
1106  merge_date(metadata);
1107 }
1108 
1110  const char *magic, ID3v2ExtraMeta **extra_meta)
1111 {
1112  id3v2_read_internal(pb, metadata, NULL, magic, extra_meta, 0);
1113 }
1114 
1115 void ff_id3v2_read(AVFormatContext *s, const char *magic,
1116  ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
1117 {
1118  id3v2_read_internal(s->pb, &s->metadata, s, magic, extra_meta, max_search_size);
1119 }
1120 
1122 {
1123  ID3v2ExtraMeta *current = *extra_meta, *next;
1124  const ID3v2EMFunc *extra_func;
1125 
1126  while (current) {
1127  if ((extra_func = get_extra_meta_func(current->tag, 1)))
1128  extra_func->free(current->data);
1129  next = current->next;
1130  av_freep(&current);
1131  current = next;
1132  }
1133 
1134  *extra_meta = NULL;
1135 }
1136 
1138 {
1139  ID3v2ExtraMeta *cur;
1140 
1141  for (cur = *extra_meta; cur; cur = cur->next) {
1142  ID3v2ExtraMetaAPIC *apic;
1143  AVStream *st;
1144 
1145  if (strcmp(cur->tag, "APIC"))
1146  continue;
1147  apic = cur->data;
1148 
1149  if (!(st = avformat_new_stream(s, NULL)))
1150  return AVERROR(ENOMEM);
1151 
1154  st->codecpar->codec_id = apic->id;
1155 
1156  if (AV_RB64(apic->buf->data) == 0x89504e470d0a1a0a)
1158 
1159  if (apic->description[0])
1160  av_dict_set(&st->metadata, "title", apic->description, 0);
1161 
1162  av_dict_set(&st->metadata, "comment", apic->type, 0);
1163 
1165  st->attached_pic.buf = apic->buf;
1166  st->attached_pic.data = apic->buf->data;
1168  st->attached_pic.stream_index = st->index;
1170 
1171  apic->buf = NULL;
1172  }
1173 
1174  return 0;
1175 }
1176 
1178 {
1179  int ret = 0;
1180  ID3v2ExtraMeta *cur;
1181  AVRational time_base = {1, 1000};
1182  ID3v2ExtraMetaCHAP **chapters = NULL;
1183  int num_chapters = 0;
1184  int i;
1185 
1186  // since extra_meta is a linked list where elements are prepended,
1187  // we need to reverse the order of chapters
1188  for (cur = *extra_meta; cur; cur = cur->next) {
1189  ID3v2ExtraMetaCHAP *chap;
1190 
1191  if (strcmp(cur->tag, "CHAP"))
1192  continue;
1193  chap = cur->data;
1194 
1195  if ((ret = av_dynarray_add_nofree(&chapters, &num_chapters, chap)) < 0)
1196  goto end;
1197  }
1198 
1199  for (i = 0; i < (num_chapters / 2); i++) {
1200  ID3v2ExtraMetaCHAP *right;
1201  int right_index;
1202 
1203  right_index = (num_chapters - 1) - i;
1204  right = chapters[right_index];
1205 
1206  chapters[right_index] = chapters[i];
1207  chapters[i] = right;
1208  }
1209 
1210  for (i = 0; i < num_chapters; i++) {
1211  ID3v2ExtraMetaCHAP *chap;
1212  AVChapter *chapter;
1213 
1214  chap = chapters[i];
1215  chapter = avpriv_new_chapter(s, i, time_base, chap->start, chap->end, chap->element_id);
1216  if (!chapter)
1217  continue;
1218 
1219  if ((ret = av_dict_copy(&chapter->metadata, chap->meta, 0)) < 0)
1220  goto end;
1221  }
1222 
1223 end:
1224  av_freep(&chapters);
1225  return ret;
1226 }
#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:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVDictionary * meta
Definition: id3v2.h:85
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:125
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1342
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:791
static void id3v2_parse(AVIOContext *pb, AVDictionary **metadata, AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
Definition: id3v2.c:818
const char * tag3
Definition: id3v2.c:783
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
uint8_t * element_id
Definition: id3v2.h:83
uint32_t datasize
Definition: id3v2.h:62
int index
stream index in AVFormatContext
Definition: avformat.h:890
int size
Definition: avcodec.h:1680
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:244
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:223
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
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:329
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:222
#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:775
AVDictionary * metadata
Definition: avformat.h:1310
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:1137
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1313
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:4494
Format I/O context.
Definition: avformat.h:1349
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:785
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:1063
#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)
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition: id3v2.c:1177
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:790
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
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_X86ASM &&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
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen, const char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
Definition: id3v2.c:745
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
uint32_t end
Definition: id3v2.h:84
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:1679
const char * type
Definition: id3v2.h:71
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1409
enum AVCodecID id
Definition: internal.h:51
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:280
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:556
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
uint8_t * data
Definition: id3v2.h:66
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1711
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:214
#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:1567
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:737
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1662
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1121
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:109
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1685
static unsigned int size_to_syncsafe(unsigned int size)
Definition: id3v2.c:174
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
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:469
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:129
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:783
#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
uint32_t start
Definition: id3v2.h:84
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:961
uint8_t * owner
Definition: id3v2.h:77
static void error(const char *err)
#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:1372
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:237
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:67
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:194
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:255
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:804
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:974
static void free_chapter(void *obj)
Definition: id3v2.c:673
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
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
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:950
#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:784
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.
Definition: id3v2.c:1115
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:777
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:1109
char * value
Definition: dict.h:87
int eof_reached
true if eof reached
Definition: avio.h:240
int len
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:788
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
AVCodecParameters * codecpar
Definition: avformat.h:1252
const char *const ff_id3v2_picture_types[21]
Definition: id3v2.c:105
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:815
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:751
#define ID3v2_FLAG_DATALEN
Definition: id3v2.h:37
int stream_index
Definition: avcodec.h:1681
#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
enum AVCodecID id
static void read_chapter(AVFormatContext *s, AVIOContext *pb, int len, const char *ttag, ID3v2ExtraMeta **extra_meta, int isv34)
Definition: id3v2.c:681
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:979
GLuint buffer
Definition: opengl_enc.c:102
#define av_unused
Definition: attributes.h:125
static const AVMetadataConv id3v2_2_metadata_conv[]
Definition: id3v2.c:73
static uint8_t tmp[11]
Definition: aes_ctr.c:26