Libav
id3v2.c
Go to the documentation of this file.
1 /*
2  * ID3v2 header parser
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/avstring.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/intreadwrite.h"
25 #include "avio_internal.h"
26 #include "internal.h"
27 #include "id3v1.h"
28 #include "id3v2.h"
29 
31  { "TALB", "album" },
32  { "TCOM", "composer" },
33  { "TCON", "genre" },
34  { "TCOP", "copyright" },
35  { "TENC", "encoded_by" },
36  { "TIT2", "title" },
37  { "TLAN", "language" },
38  { "TPE1", "artist" },
39  { "TPE2", "album_artist" },
40  { "TPE3", "performer" },
41  { "TPOS", "disc" },
42  { "TPUB", "publisher" },
43  { "TRCK", "track" },
44  { "TSSE", "encoder" },
45  { 0 }
46 };
47 
49  { "TDRL", "date" },
50  { "TDRC", "date" },
51  { "TDEN", "creation_time" },
52  { "TSOA", "album-sort" },
53  { "TSOP", "artist-sort" },
54  { "TSOT", "title-sort" },
55  { 0 }
56 };
57 
59  { "TAL", "album" },
60  { "TCO", "genre" },
61  { "TT2", "title" },
62  { "TEN", "encoded_by" },
63  { "TP1", "artist" },
64  { "TP2", "album_artist" },
65  { "TP3", "performer" },
66  { "TRK", "track" },
67  { 0 }
68 };
69 
70 const char ff_id3v2_tags[][4] = {
71  "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
72  "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
73  "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
74  "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
75  { 0 },
76 };
77 
78 const char ff_id3v2_4_tags[][4] = {
79  "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
80  "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
81  { 0 },
82 };
83 
84 const char ff_id3v2_3_tags[][4] = {
85  "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
86  { 0 },
87 };
88 
89 const char *ff_id3v2_picture_types[21] = {
90  "Other",
91  "32x32 pixels 'file icon'",
92  "Other file icon",
93  "Cover (front)",
94  "Cover (back)",
95  "Leaflet page",
96  "Media (e.g. label side of CD)",
97  "Lead artist/lead performer/soloist",
98  "Artist/performer",
99  "Conductor",
100  "Band/Orchestra",
101  "Composer",
102  "Lyricist/text writer",
103  "Recording Location",
104  "During recording",
105  "During performance",
106  "Movie/video screen capture",
107  "A bright coloured fish",
108  "Illustration",
109  "Band/artist logotype",
110  "Publisher/Studio logotype",
111 };
112 
114  { "image/gif", AV_CODEC_ID_GIF },
115  { "image/jpeg", AV_CODEC_ID_MJPEG },
116  { "image/jpg", AV_CODEC_ID_MJPEG },
117  { "image/png", AV_CODEC_ID_PNG },
118  { "image/tiff", AV_CODEC_ID_TIFF },
119  { "image/bmp", AV_CODEC_ID_BMP },
120  { "JPG", AV_CODEC_ID_MJPEG }, /* ID3v2.2 */
121  { "PNG", AV_CODEC_ID_PNG }, /* ID3v2.2 */
122  { "", AV_CODEC_ID_NONE },
123 };
124 
125 int ff_id3v2_match(const uint8_t *buf, const char *magic)
126 {
127  return buf[0] == magic[0] &&
128  buf[1] == magic[1] &&
129  buf[2] == magic[2] &&
130  buf[3] != 0xff &&
131  buf[4] != 0xff &&
132  (buf[6] & 0x80) == 0 &&
133  (buf[7] & 0x80) == 0 &&
134  (buf[8] & 0x80) == 0 &&
135  (buf[9] & 0x80) == 0;
136 }
137 
138 int ff_id3v2_tag_len(const uint8_t *buf)
139 {
140  int len = ((buf[6] & 0x7f) << 21) +
141  ((buf[7] & 0x7f) << 14) +
142  ((buf[8] & 0x7f) << 7) +
143  (buf[9] & 0x7f) +
145  if (buf[5] & 0x10)
146  len += ID3v2_HEADER_SIZE;
147  return len;
148 }
149 
150 static unsigned int get_size(AVIOContext *s, int len)
151 {
152  int v = 0;
153  while (len--)
154  v = (v << 7) + (avio_r8(s) & 0x7F);
155  return v;
156 }
157 
161 static void free_geobtag(void *obj)
162 {
163  ID3v2ExtraMetaGEOB *geob = obj;
164  av_free(geob->mime_type);
165  av_free(geob->file_name);
166  av_free(geob->description);
167  av_free(geob->data);
168  av_free(geob);
169 }
170 
183 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
184  uint8_t **dst, int *maxread)
185 {
186  int ret;
187  uint8_t tmp;
188  uint32_t ch = 1;
189  int left = *maxread;
190  unsigned int (*get)(AVIOContext*) = avio_rb16;
191  AVIOContext *dynbuf;
192 
193  if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
194  av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
195  return ret;
196  }
197 
198  switch (encoding) {
200  while (left && ch) {
201  ch = avio_r8(pb);
202  PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
203  left--;
204  }
205  break;
206 
208  if ((left -= 2) < 0) {
209  av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
210  avio_close_dyn_buf(dynbuf, dst);
211  av_freep(dst);
212  return AVERROR_INVALIDDATA;
213  }
214  switch (avio_rb16(pb)) {
215  case 0xfffe:
216  get = avio_rl16;
217  case 0xfeff:
218  break;
219  default:
220  av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
221  avio_close_dyn_buf(dynbuf, dst);
222  av_freep(dst);
223  *maxread = left;
224  return AVERROR_INVALIDDATA;
225  }
226  // fall-through
227 
229  while ((left > 1) && ch) {
230  GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
231  PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
232  }
233  if (left < 0)
234  left += 2; /* did not read last char from pb */
235  break;
236 
237  case ID3v2_ENCODING_UTF8:
238  while (left && ch) {
239  ch = avio_r8(pb);
240  avio_w8(dynbuf, ch);
241  left--;
242  }
243  break;
244  default:
245  av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
246  }
247 
248  if (ch)
249  avio_w8(dynbuf, 0);
250 
251  avio_close_dyn_buf(dynbuf, dst);
252  *maxread = left;
253 
254  return 0;
255 }
256 
260 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen,
261  const char *key)
262 {
263  uint8_t *dst;
264  int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
265  unsigned genre;
266 
267  if (taglen < 1)
268  return;
269 
270  encoding = avio_r8(pb);
271  taglen--; /* account for encoding type byte */
272 
273  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
274  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
275  return;
276  }
277 
278  if (!(strcmp(key, "TCON") && strcmp(key, "TCO")) &&
279  (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1) &&
280  genre <= ID3v1_GENRE_MAX) {
281  av_freep(&dst);
282  dst = av_strdup(ff_id3v1_genre_str[genre]);
283  } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
284  /* dst now contains the key, need to get value */
285  key = dst;
286  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
287  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
288  av_freep(&key);
289  return;
290  }
291  dict_flags |= AV_DICT_DONT_STRDUP_KEY;
292  } else if (!*dst)
293  av_freep(&dst);
294 
295  if (dst)
296  av_dict_set(&s->metadata, key, dst, dict_flags);
297 }
298 
302 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen,
303  char *tag, ID3v2ExtraMeta **extra_meta)
304 {
305  ID3v2ExtraMetaGEOB *geob_data = NULL;
306  ID3v2ExtraMeta *new_extra = NULL;
307  char encoding;
308  unsigned int len;
309 
310  if (taglen < 1)
311  return;
312 
313  geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
314  if (!geob_data) {
315  av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n",
316  sizeof(ID3v2ExtraMetaGEOB));
317  return;
318  }
319 
320  new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
321  if (!new_extra) {
322  av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n",
323  sizeof(ID3v2ExtraMeta));
324  goto fail;
325  }
326 
327  /* read encoding type byte */
328  encoding = avio_r8(pb);
329  taglen--;
330 
331  /* read MIME type (always ISO-8859) */
332  if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type,
333  &taglen) < 0 ||
334  taglen <= 0)
335  goto fail;
336 
337  /* read file name */
338  if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0 ||
339  taglen <= 0)
340  goto fail;
341 
342  /* read content description */
343  if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0 ||
344  taglen < 0)
345  goto fail;
346 
347  if (taglen) {
348  /* save encapsulated binary data */
349  geob_data->data = av_malloc(taglen);
350  if (!geob_data->data) {
351  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
352  goto fail;
353  }
354  if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
356  "Error reading GEOB frame, data truncated.\n");
357  geob_data->datasize = len;
358  } else {
359  geob_data->data = NULL;
360  geob_data->datasize = 0;
361  }
362 
363  /* add data to the list */
364  new_extra->tag = "GEOB";
365  new_extra->data = geob_data;
366  new_extra->next = *extra_meta;
367  *extra_meta = new_extra;
368 
369  return;
370 
371 fail:
372  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
373  free_geobtag(geob_data);
374  av_free(new_extra);
375  return;
376 }
377 
378 static int is_number(const char *str)
379 {
380  while (*str >= '0' && *str <= '9')
381  str++;
382  return !*str;
383 }
384 
386 {
388  if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
389  strlen(t->value) == 4 && is_number(t->value))
390  return t;
391  return NULL;
392 }
393 
394 static void merge_date(AVDictionary **m)
395 {
397  char date[17] = { 0 }; // YYYY-MM-DD hh:mm
398 
399  if (!(t = get_date_tag(*m, "TYER")) &&
400  !(t = get_date_tag(*m, "TYE")))
401  return;
402  av_strlcpy(date, t->value, 5);
403  av_dict_set(m, "TYER", NULL, 0);
404  av_dict_set(m, "TYE", NULL, 0);
405 
406  if (!(t = get_date_tag(*m, "TDAT")) &&
407  !(t = get_date_tag(*m, "TDA")))
408  goto finish;
409  snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
410  av_dict_set(m, "TDAT", NULL, 0);
411  av_dict_set(m, "TDA", NULL, 0);
412 
413  if (!(t = get_date_tag(*m, "TIME")) &&
414  !(t = get_date_tag(*m, "TIM")))
415  goto finish;
416  snprintf(date + 10, sizeof(date) - 10,
417  " %.2s:%.2s", t->value, t->value + 2);
418  av_dict_set(m, "TIME", NULL, 0);
419  av_dict_set(m, "TIM", NULL, 0);
420 
421 finish:
422  if (date[0])
423  av_dict_set(m, "date", date, 0);
424 }
425 
426 static void free_apic(void *obj)
427 {
428  ID3v2ExtraMetaAPIC *apic = obj;
429  av_buffer_unref(&apic->buf);
430  av_freep(&apic->description);
431  av_freep(&apic);
432 }
433 
434 static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen,
435  char *tag, ID3v2ExtraMeta **extra_meta)
436 {
437  int enc, pic_type;
438  char mimetype[64];
439  const CodecMime *mime = ff_id3v2_mime_tags;
440  enum AVCodecID id = AV_CODEC_ID_NONE;
441  ID3v2ExtraMetaAPIC *apic = NULL;
442  ID3v2ExtraMeta *new_extra = NULL;
443  int64_t end = avio_tell(pb) + taglen;
444 
445  if (taglen <= 4)
446  goto fail;
447 
448  new_extra = av_mallocz(sizeof(*new_extra));
449  apic = av_mallocz(sizeof(*apic));
450  if (!new_extra || !apic)
451  goto fail;
452 
453  enc = avio_r8(pb);
454  taglen--;
455 
456  /* mimetype */
457  taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
458  while (mime->id != AV_CODEC_ID_NONE) {
459  if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
460  id = mime->id;
461  break;
462  }
463  mime++;
464  }
465  if (id == AV_CODEC_ID_NONE) {
467  "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
468  goto fail;
469  }
470  apic->id = id;
471 
472  /* picture type */
473  pic_type = avio_r8(pb);
474  taglen--;
475  if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
476  av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n",
477  pic_type);
478  pic_type = 0;
479  }
480  apic->type = ff_id3v2_picture_types[pic_type];
481 
482  /* description and picture data */
483  if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
484  av_log(s, AV_LOG_ERROR,
485  "Error decoding attached picture description.\n");
486  goto fail;
487  }
488 
490  if (!apic->buf || avio_read(pb, apic->buf->data, taglen) != taglen)
491  goto fail;
492  memset(apic->buf->data + taglen, 0, FF_INPUT_BUFFER_PADDING_SIZE);
493 
494  new_extra->tag = "APIC";
495  new_extra->data = apic;
496  new_extra->next = *extra_meta;
497  *extra_meta = new_extra;
498 
499  return;
500 
501 fail:
502  if (apic)
503  free_apic(apic);
504  av_freep(&new_extra);
505  avio_seek(pb, end, SEEK_SET);
506 }
507 
508 typedef struct ID3v2EMFunc {
509  const char *tag3;
510  const char *tag4;
511  void (*read)(AVFormatContext *, AVIOContext *, int, char *,
512  ID3v2ExtraMeta **);
513  void (*free)(void *obj);
514 } ID3v2EMFunc;
515 
517  { "GEO", "GEOB", read_geobtag, free_geobtag },
518  { "PIC", "APIC", read_apic, free_apic },
519  { NULL }
520 };
521 
527 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
528 {
529  int i = 0;
530  while (id3v2_extra_meta_funcs[i].tag3) {
531  if (!memcmp(tag,
532  (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
533  id3v2_extra_meta_funcs[i].tag3),
534  (isv34 ? 4 : 3)))
535  return &id3v2_extra_meta_funcs[i];
536  i++;
537  }
538  return NULL;
539 }
540 
542  uint8_t flags, ID3v2ExtraMeta **extra_meta)
543 {
544  int isv34, tlen, unsync;
545  char tag[5];
546  int64_t next, end = avio_tell(s->pb) + len;
547  int taghdrlen;
548  const char *reason = NULL;
549  AVIOContext pb;
550  AVIOContext *pbx;
551  unsigned char *buffer = NULL;
552  int buffer_size = 0;
553  const ID3v2EMFunc *extra_func;
554 
555  switch (version) {
556  case 2:
557  if (flags & 0x40) {
558  reason = "compression";
559  goto error;
560  }
561  isv34 = 0;
562  taghdrlen = 6;
563  break;
564 
565  case 3:
566  case 4:
567  isv34 = 1;
568  taghdrlen = 10;
569  break;
570 
571  default:
572  reason = "version";
573  goto error;
574  }
575 
576  unsync = flags & 0x80;
577 
578  if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
579  int extlen = get_size(s->pb, 4);
580  if (version == 4)
581  /* In v2.4 the length includes the length field we just read. */
582  extlen -= 4;
583 
584  if (extlen < 0) {
585  reason = "invalid extended header length";
586  goto error;
587  }
588  avio_skip(s->pb, extlen);
589  }
590 
591  while (len >= taghdrlen) {
592  unsigned int tflags = 0;
593  int tunsync = 0;
594 
595  if (isv34) {
596  avio_read(s->pb, tag, 4);
597  tag[4] = 0;
598  if (version == 3) {
599  tlen = avio_rb32(s->pb);
600  } else
601  tlen = get_size(s->pb, 4);
602  tflags = avio_rb16(s->pb);
603  tunsync = tflags & ID3v2_FLAG_UNSYNCH;
604  } else {
605  avio_read(s->pb, tag, 3);
606  tag[3] = 0;
607  tlen = avio_rb24(s->pb);
608  }
609  if (tlen < 0 || tlen > len - taghdrlen) {
611  "Invalid size in frame %s, skipping the rest of tag.\n",
612  tag);
613  break;
614  }
615  len -= taghdrlen + tlen;
616  next = avio_tell(s->pb) + tlen;
617 
618  if (!tlen) {
619  if (tag[0])
620  av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n",
621  tag);
622  continue;
623  }
624 
625  if (tflags & ID3v2_FLAG_DATALEN) {
626  avio_rb32(s->pb);
627  tlen -= 4;
628  }
629 
632  "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
633  avio_skip(s->pb, tlen);
634  /* check for text tag or supported special meta tag */
635  } else if (tag[0] == 'T' ||
636  (extra_meta &&
637  (extra_func = get_extra_meta_func(tag, isv34)))) {
638  if (unsync || tunsync) {
639  int64_t end = avio_tell(s->pb) + tlen;
640  uint8_t *b;
641  av_fast_malloc(&buffer, &buffer_size, tlen);
642  if (!buffer) {
643  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
644  goto seek;
645  }
646  b = buffer;
647  while (avio_tell(s->pb) < end && !s->pb->eof_reached) {
648  *b++ = avio_r8(s->pb);
649  if (*(b - 1) == 0xff && avio_tell(s->pb) < end - 1 &&
650  !s->pb->eof_reached ) {
651  uint8_t val = avio_r8(s->pb);
652  *b++ = val ? val : avio_r8(s->pb);
653  }
654  }
655  ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL,
656  NULL);
657  tlen = b - buffer;
658  pbx = &pb; // read from sync buffer
659  } else {
660  pbx = s->pb; // read straight from input
661  }
662  if (tag[0] == 'T')
663  /* parse text tag */
664  read_ttag(s, pbx, tlen, tag);
665  else
666  /* parse special meta tag */
667  extra_func->read(s, pbx, tlen, tag, extra_meta);
668  } else if (!tag[0]) {
669  if (tag[1])
670  av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
671  avio_skip(s->pb, tlen);
672  break;
673  }
674  /* Skip to end of tag */
675 seek:
676  avio_seek(s->pb, next, SEEK_SET);
677  }
678 
679  /* Footer preset, always 10 bytes, skip over it */
680  if (version == 4 && flags & 0x10)
681  end += 10;
682 
683 error:
684  if (reason)
685  av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n",
686  version, reason);
687  avio_seek(s->pb, end, SEEK_SET);
688  av_free(buffer);
689  return;
690 }
691 
692 void ff_id3v2_read(AVFormatContext *s, const char *magic,
693  ID3v2ExtraMeta **extra_meta)
694 {
695  int len, ret;
697  int found_header;
698  int64_t off;
699 
700  do {
701  /* save the current offset in case there's nothing to read/skip */
702  off = avio_tell(s->pb);
703  ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
704  if (ret != ID3v2_HEADER_SIZE)
705  break;
706  found_header = ff_id3v2_match(buf, magic);
707  if (found_header) {
708  /* parse ID3v2 header */
709  len = ((buf[6] & 0x7f) << 21) |
710  ((buf[7] & 0x7f) << 14) |
711  ((buf[8] & 0x7f) << 7) |
712  (buf[9] & 0x7f);
713  id3v2_parse(s, len, buf[3], buf[5], extra_meta);
714  } else {
715  avio_seek(s->pb, off, SEEK_SET);
716  }
717  } while (found_header);
718  ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
719  ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
720  ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
721  merge_date(&s->metadata);
722 }
723 
725 {
726  ID3v2ExtraMeta *current = *extra_meta, *next;
727  const ID3v2EMFunc *extra_func;
728 
729  while (current) {
730  if ((extra_func = get_extra_meta_func(current->tag, 1)))
731  extra_func->free(current->data);
732  next = current->next;
733  av_freep(&current);
734  current = next;
735  }
736 }
737 
739 {
740  ID3v2ExtraMeta *cur;
741 
742  for (cur = *extra_meta; cur; cur = cur->next) {
743  ID3v2ExtraMetaAPIC *apic;
744  AVStream *st;
745 
746  if (strcmp(cur->tag, "APIC"))
747  continue;
748  apic = cur->data;
749 
750  if (!(st = avformat_new_stream(s, NULL)))
751  return AVERROR(ENOMEM);
752 
755  st->codec->codec_id = apic->id;
756  av_dict_set(&st->metadata, "title", apic->description, 0);
757  av_dict_set(&st->metadata, "comment", apic->type, 0);
758 
760  st->attached_pic.buf = apic->buf;
761  st->attached_pic.data = apic->buf->data;
763  st->attached_pic.stream_index = st->index;
765 
766  apic->buf = NULL;
767  }
768 
769  return 0;
770 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
void * data
Definition: id3v2.h:57
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
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:106
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:977
enum AVCodecID id
Definition: mxfenc.c:84
static int is_number(const char *str)
Definition: id3v2.c:378
static AVDictionaryEntry * get_date_tag(AVDictionary *m, const char *tag)
Definition: id3v2.c:385
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:69
static const ID3v2EMFunc id3v2_extra_meta_funcs[]
Definition: id3v2.c:516
const char * tag3
Definition: id3v2.c:509
void(* read)(AVFormatContext *, AVIOContext *, int, char *, ID3v2ExtraMeta **)
Definition: id3v2.c:511
uint32_t datasize
Definition: id3v2.h:62
int index
stream index in AVFormatContext
Definition: avformat.h:700
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
Definition: id3v2.c:434
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:166
static void free_apic(void *obj)
Definition: id3v2.c:426
const char ff_id3v2_4_tags[][4]
ID3v2.4-only text information frames.
Definition: id3v2.c:78
#define FF_ARRAY_ELEMS(a)
#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:305
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:591
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:738
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:965
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:183
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() and children.
Definition: dict.h:63
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:922
char str[32]
Definition: internal.h:41
Public dictionary API.
#define ID3v2_HEADER_SIZE
Definition: id3v2.h:30
const AVMetadataConv ff_id3v2_34_metadata_conv[]
Definition: id3v2.c:30
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:606
#define b
Definition: input.c:52
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
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:38
uint8_t * data
Definition: avcodec.h:973
const char * type
Definition: id3v2.h:71
static int flags
Definition: log.c:44
uint32_t tag
Definition: movenc.c:844
enum AVCodecID id
Definition: internal.h:42
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:463
uint8_t * data
Definition: id3v2.h:66
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
#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:277
static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
Definition: id3v2.c:302
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AV_DICT_MATCH_CASE
Definition: dict.h:61
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
const AVMetadataConv ff_id3v2_4_metadata_conv[]
Definition: id3v2.c:48
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:724
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
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:81
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:89
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:454
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:113
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:599
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() and chilren.
Definition: dict.h:66
static void free_geobtag(void *obj)
Free GEOB type extra metadata.
Definition: id3v2.c:161
static unsigned int get_size(AVIOContext *s, int len)
Definition: id3v2.c:150
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
static char buffer[20]
Definition: seek-test.c:31
#define ID3v2_FLAG_UNSYNCH
Definition: id3v2.h:38
AVDictionary * metadata
Definition: avformat.h:771
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:690
static void merge_date(AVDictionary **m)
Definition: id3v2.c:394
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag, including supported extra metadata.
Definition: id3v2.c:692
Stream structure.
Definition: avformat.h:699
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:70
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
enum AVMediaType codec_type
Definition: avcodec.h:1058
struct ID3v2ExtraMeta * next
Definition: id3v2.h:58
version
Definition: ffv1enc.c:1080
enum AVCodecID codec_id
Definition: avcodec.h:1067
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
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:964
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
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
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:68
static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
Parse a text tag.
Definition: id3v2.c:260
static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
Definition: id3v2.c:541
int size
Size of data in bytes.
Definition: buffer.h:93
static const ID3v2EMFunc * get_extra_meta_func(const char *tag, int isv34)
Get the corresponding ID3v2EMFunc struct for a tag.
Definition: id3v2.c:527
uint8_t * mime_type
Definition: id3v2.h:63
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:559
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:388
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition: id3v2.c:125
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:70
const char * tag
Definition: id3v2.h:56
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:760
#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:47
const char * tag4
Definition: id3v2.c:510
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
const char ff_id3v2_3_tags[][4]
ID3v2.3-only text information frames.
Definition: id3v2.c:84
uint8_t * file_name
Definition: id3v2.h:64
char * value
Definition: dict.h:76
int eof_reached
true if eof reached
Definition: avio.h:96
int len
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition: id3v2.c:138
AVBufferRef * buf
Definition: id3v2.h:70
void(* free)(void *obj)
Definition: id3v2.c:513
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:629
#define ID3v2_FLAG_DATALEN
Definition: id3v2.h:37
int stream_index
Definition: avcodec.h:975
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:26
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:789
static const AVMetadataConv id3v2_2_metadata_conv[]
Definition: id3v2.c:58