FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
id3v2enc.c
Go to the documentation of this file.
1 /*
2  * ID3v2 header writer
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 #include <stdint.h>
22 #include <string.h>
23 
24 #include "libavutil/avstring.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/intreadwrite.h"
27 #include "avformat.h"
28 #include "avio.h"
29 #include "avio_internal.h"
30 #include "id3v2.h"
31 
32 #define PADDING_BYTES 10
33 
34 static void id3v2_put_size(AVIOContext *pb, int size)
35 {
36  avio_w8(pb, size >> 21 & 0x7f);
37  avio_w8(pb, size >> 14 & 0x7f);
38  avio_w8(pb, size >> 7 & 0x7f);
39  avio_w8(pb, size & 0x7f);
40 }
41 
42 static int string_is_ascii(const uint8_t *str)
43 {
44  while (*str && *str < 128) str++;
45  return !*str;
46 }
47 
48 static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
49  enum ID3v2Encoding enc)
50 {
51  int (*put)(AVIOContext*, const char*);
52 
53  if (enc == ID3v2_ENCODING_UTF16BOM) {
54  avio_wl16(pb, 0xFEFF); /* BOM */
55  put = avio_put_str16le;
56  } else
57  put = avio_put_str;
58 
59  put(pb, str);
60 }
61 
62 /**
63  * Write a text frame with one (normal frames) or two (TXXX frames) strings
64  * according to encoding (only UTF-8 or UTF-16+BOM supported).
65  * @return number of bytes written or a negative error code.
66  */
67 static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
68  uint32_t tag, enum ID3v2Encoding enc)
69 {
70  int len;
71  uint8_t *pb;
72  AVIOContext *dyn_buf;
73  if (avio_open_dyn_buf(&dyn_buf) < 0)
74  return AVERROR(ENOMEM);
75 
76  /* check if the strings are ASCII-only and use UTF16 only if
77  * they're not */
78  if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
79  (!str2 || string_is_ascii(str2)))
81 
82  avio_w8(dyn_buf, enc);
83  id3v2_encode_string(dyn_buf, str1, enc);
84  if (str2)
85  id3v2_encode_string(dyn_buf, str2, enc);
86  len = avio_close_dyn_buf(dyn_buf, &pb);
87 
88  avio_wb32(avioc, tag);
89  /* ID3v2.3 frame size is not synchsafe */
90  if (id3->version == 3)
91  avio_wb32(avioc, len);
92  else
93  id3v2_put_size(avioc, len);
94  avio_wb16(avioc, 0);
95  avio_write(avioc, pb, len);
96 
97  av_freep(&pb);
98  return len + ID3v2_HEADER_SIZE;
99 }
100 
102  const char table[][4], enum ID3v2Encoding enc)
103 {
104  uint32_t tag;
105  int i;
106 
107  if (t->key[0] != 'T' || strlen(t->key) != 4)
108  return -1;
109  tag = AV_RB32(t->key);
110  for (i = 0; *table[i]; i++)
111  if (tag == AV_RB32(table[i]))
112  return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
113  return -1;
114 }
115 
117 {
118  AVDictionaryEntry *mtag = NULL;
119  AVDictionary *dst = NULL;
120  const char *key, *value;
121  char year[5] = {0}, day_month[5] = {0};
122  int i;
123 
124  while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
125  key = mtag->key;
126  if (!av_strcasecmp(key, "date")) {
127  /* split date tag using "YYYY-MM-DD" format into year and month/day segments */
128  value = mtag->value;
129  i = 0;
130  while (value[i] >= '0' && value[i] <= '9') i++;
131  if (value[i] == '\0' || value[i] == '-') {
132  av_strlcpy(year, value, sizeof(year));
133  av_dict_set(&dst, "TYER", year, 0);
134 
135  if (value[i] == '-' &&
136  value[i+1] >= '0' && value[i+1] <= '1' &&
137  value[i+2] >= '0' && value[i+2] <= '9' &&
138  value[i+3] == '-' &&
139  value[i+4] >= '0' && value[i+4] <= '3' &&
140  value[i+5] >= '0' && value[i+5] <= '9' &&
141  (value[i+6] == '\0' || value[i+6] == ' ')) {
142  snprintf(day_month, sizeof(day_month), "%.2s%.2s", value + i + 4, value + i + 1);
143  av_dict_set(&dst, "TDAT", day_month, 0);
144  }
145  } else
146  av_dict_set(&dst, key, value, 0);
147  } else
148  av_dict_set(&dst, key, mtag->value, 0);
149  }
150  av_dict_free(pm);
151  *pm = dst;
152 }
153 
154 void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
155  const char *magic)
156 {
157  id3->version = id3v2_version;
158 
159  avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
160  avio_w8(pb, 0);
161  avio_w8(pb, 0); /* flags */
162 
163  /* reserve space for size */
164  id3->size_pos = avio_tell(pb);
165  avio_wb32(pb, 0);
166 }
167 
168 static int write_metadata(AVIOContext *pb, AVDictionary **metadata,
169  ID3v2EncContext *id3, int enc)
170 {
171  AVDictionaryEntry *t = NULL;
172  int ret;
173 
175  if (id3->version == 3)
176  id3v2_3_metadata_split_date(metadata);
177  else if (id3->version == 4)
179 
180  while ((t = av_dict_get(*metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
181  if ((ret = id3v2_check_write_tag(id3, pb, t, ff_id3v2_tags, enc)) > 0) {
182  id3->len += ret;
183  continue;
184  }
185  if ((ret = id3v2_check_write_tag(id3, pb, t, id3->version == 3 ?
186  ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
187  id3->len += ret;
188  continue;
189  }
190 
191  /* unknown tag, write as TXXX frame */
192  if ((ret = id3v2_put_ttag(id3, pb, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
193  return ret;
194  id3->len += ret;
195  }
196 
197  return 0;
198 }
199 
200 static int write_chapter(AVFormatContext *s, ID3v2EncContext *id3, int id, int enc)
201 {
202  const AVRational time_base = {1, 1000};
203  AVChapter *ch = s->chapters[id];
204  uint8_t *dyn_buf = NULL;
205  AVIOContext *dyn_bc = NULL;
206  char name[123];
207  int len, start, end, ret;
208 
209  if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
210  goto fail;
211 
212  start = av_rescale_q(ch->start, ch->time_base, time_base);
213  end = av_rescale_q(ch->end, ch->time_base, time_base);
214 
215  snprintf(name, 122, "ch%d", id);
216  id3->len += avio_put_str(dyn_bc, name);
217  avio_wb32(dyn_bc, start);
218  avio_wb32(dyn_bc, end);
219  avio_wb32(dyn_bc, 0xFFFFFFFFu);
220  avio_wb32(dyn_bc, 0xFFFFFFFFu);
221 
222  if ((ret = write_metadata(dyn_bc, &ch->metadata, id3, enc)) < 0)
223  goto fail;
224 
225  len = avio_close_dyn_buf(dyn_bc, &dyn_buf);
226  id3->len += 16 + ID3v2_HEADER_SIZE;
227 
228  avio_wb32(s->pb, MKBETAG('C', 'H', 'A', 'P'));
229  avio_wb32(s->pb, len);
230  avio_wb16(s->pb, 0);
231  avio_write(s->pb, dyn_buf, len);
232 
233 fail:
234  if (dyn_bc && !dyn_buf)
235  avio_close_dyn_buf(dyn_bc, &dyn_buf);
236  av_freep(&dyn_buf);
237 
238  return ret;
239 }
240 
242 {
243  int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
245  int i, ret;
246 
247  if ((ret = write_metadata(s->pb, &s->metadata, id3, enc)) < 0)
248  return ret;
249 
250  for (i = 0; i < s->nb_chapters; i++) {
251  if ((ret = write_chapter(s, id3, i, enc)) < 0)
252  return ret;
253  }
254 
255  return 0;
256 }
257 
259 {
260  AVStream *st = s->streams[pkt->stream_index];
262 
263  AVIOContext *dyn_buf;
264  uint8_t *buf;
265  const CodecMime *mime = ff_id3v2_mime_tags;
266  const char *mimetype = NULL, *desc = "";
267  int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
269  int i, len, type = 0;
270 
271  /* get the mimetype*/
272  while (mime->id != AV_CODEC_ID_NONE) {
273  if (mime->id == st->codec->codec_id) {
274  mimetype = mime->str;
275  break;
276  }
277  mime++;
278  }
279  if (!mimetype) {
280  av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
281  "write an attached picture.\n", st->index);
282  return AVERROR(EINVAL);
283  }
284 
285  /* get the picture type */
286  e = av_dict_get(st->metadata, "comment", NULL, 0);
287  for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
288  if (strstr(ff_id3v2_picture_types[i], e->value) == ff_id3v2_picture_types[i]) {
289  type = i;
290  break;
291  }
292  }
293 
294  /* get the description */
295  if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
296  desc = e->value;
297 
298  /* start writing */
299  if (avio_open_dyn_buf(&dyn_buf) < 0)
300  return AVERROR(ENOMEM);
301 
302  avio_w8(dyn_buf, enc);
303  avio_put_str(dyn_buf, mimetype);
304  avio_w8(dyn_buf, type);
305  id3v2_encode_string(dyn_buf, desc, enc);
306  avio_write(dyn_buf, pkt->data, pkt->size);
307  len = avio_close_dyn_buf(dyn_buf, &buf);
308 
309  avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
310  if (id3->version == 3)
311  avio_wb32(s->pb, len);
312  else
313  id3v2_put_size(s->pb, len);
314  avio_wb16(s->pb, 0);
315  avio_write(s->pb, buf, len);
316  av_freep(&buf);
317 
318  id3->len += len + ID3v2_HEADER_SIZE;
319 
320  return 0;
321 }
322 
324 {
325  int64_t cur_pos;
326 
327  /* adding an arbitrary amount of padding bytes at the end of the
328  * ID3 metadata fixes cover art display for some software (iTunes,
329  * Traktor, Serato, Torq) */
330  ffio_fill(pb, 0, PADDING_BYTES);
331  id3->len += PADDING_BYTES;
332 
333  cur_pos = avio_tell(pb);
334  avio_seek(pb, id3->size_pos, SEEK_SET);
335  id3v2_put_size(pb, id3->len);
336  avio_seek(pb, cur_pos, SEEK_SET);
337 }
338 
339 int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
340  const char *magic)
341 {
342  ID3v2EncContext id3 = { 0 };
343  int ret;
344 
345  ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
346  if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
347  return ret;
348  ff_id3v2_finish(&id3, s->pb);
349 
350  return 0;
351 }