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 "id3v2.h"
23 #include "id3v1.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/dict.h"
27 #include "avio_internal.h"
28 #include "internal.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 
71 const char ff_id3v2_tags[][4] = {
72  "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
73  "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
74  "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
75  "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
76  { 0 },
77 };
78 
79 const char ff_id3v2_4_tags[][4] = {
80  "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
81  "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
82  { 0 },
83 };
84 
85 const char ff_id3v2_3_tags[][4] = {
86  "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
87  { 0 },
88 };
89 
90 const char *ff_id3v2_picture_types[21] = {
91  "Other",
92  "32x32 pixels 'file icon'",
93  "Other file icon",
94  "Cover (front)",
95  "Cover (back)",
96  "Leaflet page",
97  "Media (e.g. label side of CD)",
98  "Lead artist/lead performer/soloist",
99  "Artist/performer",
100  "Conductor",
101  "Band/Orchestra",
102  "Composer",
103  "Lyricist/text writer",
104  "Recording Location",
105  "During recording",
106  "During performance",
107  "Movie/video screen capture",
108  "A bright coloured fish",
109  "Illustration",
110  "Band/artist logotype",
111  "Publisher/Studio logotype",
112 };
113 
115  {"image/gif" , AV_CODEC_ID_GIF},
116  {"image/jpeg", AV_CODEC_ID_MJPEG},
117  {"image/jpg", AV_CODEC_ID_MJPEG},
118  {"image/png" , AV_CODEC_ID_PNG},
119  {"image/tiff", AV_CODEC_ID_TIFF},
120  {"image/bmp", AV_CODEC_ID_BMP},
121  {"JPG", AV_CODEC_ID_MJPEG}, /* ID3v2.2 */
122  {"PNG" , AV_CODEC_ID_PNG}, /* ID3v2.2 */
123  {"", AV_CODEC_ID_NONE},
124 };
125 
126 int ff_id3v2_match(const uint8_t *buf, const char * magic)
127 {
128  return buf[0] == magic[0] &&
129  buf[1] == magic[1] &&
130  buf[2] == magic[2] &&
131  buf[3] != 0xff &&
132  buf[4] != 0xff &&
133  (buf[6] & 0x80) == 0 &&
134  (buf[7] & 0x80) == 0 &&
135  (buf[8] & 0x80) == 0 &&
136  (buf[9] & 0x80) == 0;
137 }
138 
139 int ff_id3v2_tag_len(const uint8_t * buf)
140 {
141  int len = ((buf[6] & 0x7f) << 21) +
142  ((buf[7] & 0x7f) << 14) +
143  ((buf[8] & 0x7f) << 7) +
144  (buf[9] & 0x7f) +
146  if (buf[5] & 0x10)
147  len += ID3v2_HEADER_SIZE;
148  return len;
149 }
150 
151 static unsigned int get_size(AVIOContext *s, int len)
152 {
153  int v = 0;
154  while (len--)
155  v = (v << 7) + (avio_r8(s) & 0x7F);
156  return v;
157 }
158 
162 static void free_geobtag(void *obj)
163 {
164  ID3v2ExtraMetaGEOB *geob = obj;
165  av_free(geob->mime_type);
166  av_free(geob->file_name);
167  av_free(geob->description);
168  av_free(geob->data);
169  av_free(geob);
170 }
171 
184 static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
185  uint8_t **dst, int *maxread)
186 {
187  int ret;
188  uint8_t tmp;
189  uint32_t ch = 1;
190  int left = *maxread;
191  unsigned int (*get)(AVIOContext*) = avio_rb16;
192  AVIOContext *dynbuf;
193 
194  if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
195  av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
196  return ret;
197  }
198 
199  switch (encoding) {
200 
202  while (left && ch) {
203  ch = avio_r8(pb);
204  PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
205  left--;
206  }
207  break;
208 
210  if ((left -= 2) < 0) {
211  av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
212  avio_close_dyn_buf(dynbuf, dst);
213  av_freep(dst);
214  return AVERROR_INVALIDDATA;
215  }
216  switch (avio_rb16(pb)) {
217  case 0xfffe:
218  get = avio_rl16;
219  case 0xfeff:
220  break;
221  default:
222  av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
223  avio_close_dyn_buf(dynbuf, dst);
224  av_freep(dst);
225  *maxread = left;
226  return AVERROR_INVALIDDATA;
227  }
228  // fall-through
229 
231  while ((left > 1) && ch) {
232  GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
233  PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
234  }
235  if (left < 0)
236  left += 2; /* did not read last char from pb */
237  break;
238 
239  case ID3v2_ENCODING_UTF8:
240  while (left && ch) {
241  ch = avio_r8(pb);
242  avio_w8(dynbuf, ch);
243  left--;
244  }
245  break;
246  default:
247  av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
248  }
249 
250  if (ch)
251  avio_w8(dynbuf, 0);
252 
253  avio_close_dyn_buf(dynbuf, dst);
254  *maxread = left;
255 
256  return 0;
257 }
258 
262 static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
263 {
264  uint8_t *dst;
265  int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
266  unsigned genre;
267 
268  if (taglen < 1)
269  return;
270 
271  encoding = avio_r8(pb);
272  taglen--; /* account for encoding type byte */
273 
274  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
275  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
276  return;
277  }
278 
279  if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
280  && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
281  && genre <= ID3v1_GENRE_MAX) {
282  av_freep(&dst);
283  dst = av_strdup(ff_id3v1_genre_str[genre]);
284  } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
285  /* dst now contains the key, need to get value */
286  key = dst;
287  if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
288  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
289  av_freep(&key);
290  return;
291  }
292  dict_flags |= AV_DICT_DONT_STRDUP_KEY;
293  } else if (!*dst)
294  av_freep(&dst);
295 
296  if (dst)
297  av_dict_set(&s->metadata, key, dst, dict_flags);
298 }
299 
303 static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, 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", sizeof(ID3v2ExtraMetaGEOB));
316  return;
317  }
318 
319  new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
320  if (!new_extra) {
321  av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
322  goto fail;
323  }
324 
325  /* read encoding type byte */
326  encoding = avio_r8(pb);
327  taglen--;
328 
329  /* read MIME type (always ISO-8859) */
330  if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
331  || taglen <= 0)
332  goto fail;
333 
334  /* read file name */
335  if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
336  || taglen <= 0)
337  goto fail;
338 
339  /* read content description */
340  if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
341  || taglen < 0)
342  goto fail;
343 
344  if (taglen) {
345  /* save encapsulated binary data */
346  geob_data->data = av_malloc(taglen);
347  if (!geob_data->data) {
348  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
349  goto fail;
350  }
351  if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
352  av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
353  geob_data->datasize = len;
354  } else {
355  geob_data->data = NULL;
356  geob_data->datasize = 0;
357  }
358 
359  /* add data to the list */
360  new_extra->tag = "GEOB";
361  new_extra->data = geob_data;
362  new_extra->next = *extra_meta;
363  *extra_meta = new_extra;
364 
365  return;
366 
367 fail:
368  av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
369  free_geobtag(geob_data);
370  av_free(new_extra);
371  return;
372 }
373 
374 static int is_number(const char *str)
375 {
376  while (*str >= '0' && *str <= '9') str++;
377  return !*str;
378 }
379 
381 {
383  if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
384  strlen(t->value) == 4 && is_number(t->value))
385  return t;
386  return NULL;
387 }
388 
389 static void merge_date(AVDictionary **m)
390 {
392  char date[17] = {0}; // YYYY-MM-DD hh:mm
393 
394  if (!(t = get_date_tag(*m, "TYER")) &&
395  !(t = get_date_tag(*m, "TYE")))
396  return;
397  av_strlcpy(date, t->value, 5);
398  av_dict_set(m, "TYER", NULL, 0);
399  av_dict_set(m, "TYE", NULL, 0);
400 
401  if (!(t = get_date_tag(*m, "TDAT")) &&
402  !(t = get_date_tag(*m, "TDA")))
403  goto finish;
404  snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
405  av_dict_set(m, "TDAT", NULL, 0);
406  av_dict_set(m, "TDA", NULL, 0);
407 
408  if (!(t = get_date_tag(*m, "TIME")) &&
409  !(t = get_date_tag(*m, "TIM")))
410  goto finish;
411  snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
412  av_dict_set(m, "TIME", NULL, 0);
413  av_dict_set(m, "TIM", NULL, 0);
414 
415 finish:
416  if (date[0])
417  av_dict_set(m, "date", date, 0);
418 }
419 
420 static void free_apic(void *obj)
421 {
422  ID3v2ExtraMetaAPIC *apic = obj;
423  av_freep(&apic->data);
424  av_freep(&apic->description);
425  av_freep(&apic);
426 }
427 
428 static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
429 {
430  int enc, pic_type;
431  char mimetype[64];
432  const CodecMime *mime = ff_id3v2_mime_tags;
433  enum AVCodecID id = AV_CODEC_ID_NONE;
434  ID3v2ExtraMetaAPIC *apic = NULL;
435  ID3v2ExtraMeta *new_extra = NULL;
436  int64_t end = avio_tell(pb) + taglen;
437 
438  if (taglen <= 4)
439  goto fail;
440 
441  new_extra = av_mallocz(sizeof(*new_extra));
442  apic = av_mallocz(sizeof(*apic));
443  if (!new_extra || !apic)
444  goto fail;
445 
446  enc = avio_r8(pb);
447  taglen--;
448 
449  /* mimetype */
450  taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
451  while (mime->id != AV_CODEC_ID_NONE) {
452  if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
453  id = mime->id;
454  break;
455  }
456  mime++;
457  }
458  if (id == AV_CODEC_ID_NONE) {
459  av_log(s, AV_LOG_WARNING, "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
460  goto fail;
461  }
462  apic->id = id;
463 
464  /* picture type */
465  pic_type = avio_r8(pb);
466  taglen--;
467  if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
468  av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n", pic_type);
469  pic_type = 0;
470  }
471  apic->type = ff_id3v2_picture_types[pic_type];
472 
473  /* description and picture data */
474  if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
475  av_log(s, AV_LOG_ERROR, "Error decoding attached picture description.\n");
476  goto fail;
477  }
478 
479  apic->len = taglen;
480  apic->data = av_malloc(taglen + FF_INPUT_BUFFER_PADDING_SIZE);
481  if (!apic->data || avio_read(pb, apic->data, taglen) != taglen)
482  goto fail;
483  memset(apic->data + taglen, 0, FF_INPUT_BUFFER_PADDING_SIZE);
484 
485  new_extra->tag = "APIC";
486  new_extra->data = apic;
487  new_extra->next = *extra_meta;
488  *extra_meta = new_extra;
489 
490  return;
491 
492 fail:
493  if (apic)
494  free_apic(apic);
495  av_freep(&new_extra);
496  avio_seek(pb, end, SEEK_SET);
497 }
498 
499 typedef struct ID3v2EMFunc {
500  const char *tag3;
501  const char *tag4;
503  void (*free)(void *obj);
504 } ID3v2EMFunc;
505 
507  { "GEO", "GEOB", read_geobtag, free_geobtag },
508  { "PIC", "APIC", read_apic, free_apic },
509  { NULL }
510 };
511 
517 static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
518 {
519  int i = 0;
520  while (id3v2_extra_meta_funcs[i].tag3) {
521  if (!memcmp(tag,
522  (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
523  id3v2_extra_meta_funcs[i].tag3),
524  (isv34 ? 4 : 3)))
525  return &id3v2_extra_meta_funcs[i];
526  i++;
527  }
528  return NULL;
529 }
530 
532 {
533  int isv34, tlen, unsync;
534  char tag[5];
535  int64_t next, end = avio_tell(s->pb) + len;
536  int taghdrlen;
537  const char *reason = NULL;
538  AVIOContext pb;
539  AVIOContext *pbx;
540  unsigned char *buffer = NULL;
541  int buffer_size = 0;
542  const ID3v2EMFunc *extra_func;
543 
544  switch (version) {
545  case 2:
546  if (flags & 0x40) {
547  reason = "compression";
548  goto error;
549  }
550  isv34 = 0;
551  taghdrlen = 6;
552  break;
553 
554  case 3:
555  case 4:
556  isv34 = 1;
557  taghdrlen = 10;
558  break;
559 
560  default:
561  reason = "version";
562  goto error;
563  }
564 
565  unsync = flags & 0x80;
566 
567  if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
568  int extlen = get_size(s->pb, 4);
569  if (version == 4)
570  extlen -= 4; // in v2.4 the length includes the length field we just read
571 
572  if (extlen < 0) {
573  reason = "invalid extended header length";
574  goto error;
575  }
576  avio_skip(s->pb, extlen);
577  }
578 
579  while (len >= taghdrlen) {
580  unsigned int tflags = 0;
581  int tunsync = 0;
582 
583  if (isv34) {
584  avio_read(s->pb, tag, 4);
585  tag[4] = 0;
586  if(version==3){
587  tlen = avio_rb32(s->pb);
588  }else
589  tlen = get_size(s->pb, 4);
590  tflags = avio_rb16(s->pb);
591  tunsync = tflags & ID3v2_FLAG_UNSYNCH;
592  } else {
593  avio_read(s->pb, tag, 3);
594  tag[3] = 0;
595  tlen = avio_rb24(s->pb);
596  }
597  if (tlen < 0 || tlen > len - taghdrlen) {
598  av_log(s, AV_LOG_WARNING, "Invalid size in frame %s, skipping the rest of tag.\n", tag);
599  break;
600  }
601  len -= taghdrlen + tlen;
602  next = avio_tell(s->pb) + tlen;
603 
604  if (!tlen) {
605  if (tag[0])
606  av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
607  continue;
608  }
609 
610  if (tflags & ID3v2_FLAG_DATALEN) {
611  avio_rb32(s->pb);
612  tlen -= 4;
613  }
614 
616  av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
617  avio_skip(s->pb, tlen);
618  /* check for text tag or supported special meta tag */
619  } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
620  if (unsync || tunsync) {
621  int64_t end = avio_tell(s->pb) + tlen;
622  uint8_t *b;
623  av_fast_malloc(&buffer, &buffer_size, tlen);
624  if (!buffer) {
625  av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
626  goto seek;
627  }
628  b = buffer;
629  while (avio_tell(s->pb) < end && !s->pb->eof_reached) {
630  *b++ = avio_r8(s->pb);
631  if (*(b - 1) == 0xff && avio_tell(s->pb) < end - 1 &&
632  !s->pb->eof_reached ) {
633  uint8_t val = avio_r8(s->pb);
634  *b++ = val ? val : avio_r8(s->pb);
635  }
636  }
637  ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL, NULL);
638  tlen = b - buffer;
639  pbx = &pb; // read from sync buffer
640  } else {
641  pbx = s->pb; // read straight from input
642  }
643  if (tag[0] == 'T')
644  /* parse text tag */
645  read_ttag(s, pbx, tlen, tag);
646  else
647  /* parse special meta tag */
648  extra_func->read(s, pbx, tlen, tag, extra_meta);
649  }
650  else if (!tag[0]) {
651  if (tag[1])
652  av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
653  avio_skip(s->pb, tlen);
654  break;
655  }
656  /* Skip to end of tag */
657 seek:
658  avio_seek(s->pb, next, SEEK_SET);
659  }
660 
661  if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
662  end += 10;
663 
664  error:
665  if (reason)
666  av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
667  avio_seek(s->pb, end, SEEK_SET);
668  av_free(buffer);
669  return;
670 }
671 
672 void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
673 {
674  int len, ret;
676  int found_header;
677  int64_t off;
678 
679  do {
680  /* save the current offset in case there's nothing to read/skip */
681  off = avio_tell(s->pb);
682  ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
683  if (ret != ID3v2_HEADER_SIZE)
684  break;
685  found_header = ff_id3v2_match(buf, magic);
686  if (found_header) {
687  /* parse ID3v2 header */
688  len = ((buf[6] & 0x7f) << 21) |
689  ((buf[7] & 0x7f) << 14) |
690  ((buf[8] & 0x7f) << 7) |
691  (buf[9] & 0x7f);
692  ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
693  } else {
694  avio_seek(s->pb, off, SEEK_SET);
695  }
696  } while (found_header);
697  ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
698  ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
699  ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
700  merge_date(&s->metadata);
701 }
702 
704 {
705  ID3v2ExtraMeta *current = *extra_meta, *next;
706  const ID3v2EMFunc *extra_func;
707 
708  while (current) {
709  if ((extra_func = get_extra_meta_func(current->tag, 1)))
710  extra_func->free(current->data);
711  next = current->next;
712  av_freep(&current);
713  current = next;
714  }
715 }
716 
718 {
719  ID3v2ExtraMeta *cur;
720 
721  for (cur = *extra_meta; cur; cur = cur->next) {
722  ID3v2ExtraMetaAPIC *apic;
723  AVStream *st;
724 
725  if (strcmp(cur->tag, "APIC"))
726  continue;
727  apic = cur->data;
728 
729  if (!(st = avformat_new_stream(s, NULL)))
730  return AVERROR(ENOMEM);
731 
734  st->codec->codec_id = apic->id;
735  av_dict_set(&st->metadata, "title", apic->description, 0);
736  av_dict_set(&st->metadata, "comment", apic->type, 0);
737 
739  st->attached_pic.data = apic->data;
740  st->attached_pic.size = apic->len;
742  st->attached_pic.stream_index = st->index;
744 
745  apic->data = NULL;
746  apic->len = 0;
747  }
748 
749  return 0;
750 }
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:61
void * data
Definition: id3v2.h:57
Bytestream IO Context.
Definition: avio.h:68
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:945
struct ID3v2EMFunc ID3v2EMFunc
enum AVCodecID id
Definition: mxfenc.c:85
static int is_number(const char *str)
Definition: id3v2.c:374
static AVDictionaryEntry * get_date_tag(AVDictionary *m, const char *tag)
Definition: id3v2.c:380
static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
Definition: id3v2.c:531
static const ID3v2EMFunc id3v2_extra_meta_funcs[]
Definition: id3v2.c:506
const char * tag3
Definition: id3v2.c:500
void(* read)(AVFormatContext *, AVIOContext *, int, char *, ID3v2ExtraMeta **)
Definition: id3v2.c:502
uint32_t datasize
Definition: id3v2.h:62
int index
stream index in AVFormatContext
Definition: avformat.h:623
int size
Definition: avcodec.h:916
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:428
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:150
static void free_apic(void *obj)
Definition: id3v2.c:420
const char ff_id3v2_4_tags[][4]
ID3v2.4-only text information frames.
Definition: id3v2.c:79
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:562
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
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:717
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:933
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:184
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:151
Format I/O context.
Definition: avformat.h:828
char str[32]
Definition: internal.h:41
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: utils.c:72
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:577
#define b
Definition: input.c:52
uint8_t * data
Definition: id3v2.h:70
uint8_t * description
Definition: id3v2.h:65
uint8_t * data
Definition: avcodec.h:915
const char * type
Definition: id3v2.h:72
static int flags
Definition: log.c:42
uint32_t tag
Definition: movenc.c:802
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
static float t
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:446
uint8_t * data
Definition: id3v2.h:66
void(* destruct)(struct AVPacket *)
Definition: avcodec.h:938
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:303
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:95
AVDictionary * metadata
Definition: avformat.h:972
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
const AVMetadataConv ff_id3v2_4_metadata_conv[]
Definition: id3v2.c:48
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:703
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2736
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:67
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:90
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
int off
Definition: dsputil_bfin.c:28
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:437
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:114
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:570
static void free_geobtag(void *obj)
Free GEOB type extra metadata.
Definition: id3v2.c:162
static unsigned int get_size(AVIOContext *s, int len)
Definition: id3v2.c:151
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:699
static void merge_date(AVDictionary **m)
Definition: id3v2.c:389
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag, including supported extra metadata.
Definition: id3v2.c:672
Stream structure.
Definition: avformat.h:622
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:71
NULL
Definition: eval.c:52
enum AVMediaType codec_type
Definition: avcodec.h:1347
struct ID3v2ExtraMeta * next
Definition: id3v2.h:58
version
Definition: ffv1enc.c:1069
enum AVCodecID codec_id
Definition: avcodec.h:1350
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:166
AVIOContext * pb
I/O context.
Definition: avformat.h:861
void av_destruct_packet(AVPacket *pkt)
Default packet destructor.
Definition: avpacket.c:28
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
#define ID3v2_FLAG_COMPRESSION
Definition: id3v2.h:40
enum AVCodecID id
Definition: id3v2.h:74
uint8_t * description
Definition: id3v2.h:73
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:63
static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
Parse a text tag.
Definition: id3v2.c:262
static const ID3v2EMFunc * get_extra_meta_func(const char *tag, int isv34)
Get the corresponding ID3v2EMFunc struct for a tag.
Definition: id3v2.c:517
uint8_t * mime_type
Definition: id3v2.h:63
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:530
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition: id3v2.c:126
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:688
#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:42
const char * tag4
Definition: id3v2.c:501
const char ff_id3v2_3_tags[][4]
ID3v2.3-only text information frames.
Definition: id3v2.c:85
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:139
void(* free)(void *obj)
Definition: id3v2.c:503
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:600
#define ID3v2_FLAG_DATALEN
Definition: id3v2.h:37
int stream_index
Definition: avcodec.h:917
#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:158
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:713
static const AVMetadataConv id3v2_2_metadata_conv[]
Definition: id3v2.c:58