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 static void id3v2_put_size(AVIOContext *pb, int size)
33 {
34  avio_w8(pb, size >> 21 & 0x7f);
35  avio_w8(pb, size >> 14 & 0x7f);
36  avio_w8(pb, size >> 7 & 0x7f);
37  avio_w8(pb, size & 0x7f);
38 }
39 
40 static int string_is_ascii(const uint8_t *str)
41 {
42  while (*str && *str < 128) str++;
43  return !*str;
44 }
45 
46 static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
47  enum ID3v2Encoding enc)
48 {
49  int (*put)(AVIOContext*, const char*);
50 
51  if (enc == ID3v2_ENCODING_UTF16BOM) {
52  avio_wl16(pb, 0xFEFF); /* BOM */
53  put = avio_put_str16le;
54  } else
55  put = avio_put_str;
56 
57  put(pb, str);
58 }
59 
60 /**
61  * Write a text frame with one (normal frames) or two (TXXX frames) strings
62  * according to encoding (only UTF-8 or UTF-16+BOM supported).
63  * @return number of bytes written or a negative error code.
64  */
65 static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
66  uint32_t tag, enum ID3v2Encoding enc)
67 {
68  int len;
69  uint8_t *pb;
70  AVIOContext *dyn_buf;
71  if (avio_open_dyn_buf(&dyn_buf) < 0)
72  return AVERROR(ENOMEM);
73 
74  /* check if the strings are ASCII-only and use UTF16 only if
75  * they're not */
76  if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
77  (!str2 || string_is_ascii(str2)))
79 
80  avio_w8(dyn_buf, enc);
81  id3v2_encode_string(dyn_buf, str1, enc);
82  if (str2)
83  id3v2_encode_string(dyn_buf, str2, enc);
84  len = avio_close_dyn_buf(dyn_buf, &pb);
85 
86  avio_wb32(avioc, tag);
87  /* ID3v2.3 frame size is not synchsafe */
88  if (id3->version == 3)
89  avio_wb32(avioc, len);
90  else
91  id3v2_put_size(avioc, len);
92  avio_wb16(avioc, 0);
93  avio_write(avioc, pb, len);
94 
95  av_freep(&pb);
96  return len + ID3v2_HEADER_SIZE;
97 }
98 
100  const char table[][4], enum ID3v2Encoding enc)
101 {
102  uint32_t tag;
103  int i;
104 
105  if (t->key[0] != 'T' || strlen(t->key) != 4)
106  return -1;
107  tag = AV_RB32(t->key);
108  for (i = 0; *table[i]; i++)
109  if (tag == AV_RB32(table[i]))
110  return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
111  return -1;
112 }
113 
115 {
116  AVDictionaryEntry *mtag = NULL;
117  AVDictionary *dst = NULL;
118  const char *key, *value;
119  char year[5] = {0}, day_month[5] = {0};
120  int i;
121 
122  while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
123  key = mtag->key;
124  if (!av_strcasecmp(key, "date")) {
125  /* split date tag using "YYYY-MM-DD" format into year and month/day segments */
126  value = mtag->value;
127  i = 0;
128  while (value[i] >= '0' && value[i] <= '9') i++;
129  if (value[i] == '\0' || value[i] == '-') {
130  av_strlcpy(year, value, sizeof(year));
131  av_dict_set(&dst, "TYER", year, 0);
132 
133  if (value[i] == '-' &&
134  value[i+1] >= '0' && value[i+1] <= '1' &&
135  value[i+2] >= '0' && value[i+2] <= '9' &&
136  value[i+3] == '-' &&
137  value[i+4] >= '0' && value[i+4] <= '3' &&
138  value[i+5] >= '0' && value[i+5] <= '9' &&
139  (value[i+6] == '\0' || value[i+6] == ' ')) {
140  snprintf(day_month, sizeof(day_month), "%.2s%.2s", value + i + 4, value + i + 1);
141  av_dict_set(&dst, "TDAT", day_month, 0);
142  }
143  } else
144  av_dict_set(&dst, key, value, 0);
145  } else
146  av_dict_set(&dst, key, mtag->value, 0);
147  }
148  av_dict_free(pm);
149  *pm = dst;
150 }
151 
152 void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
153  const char *magic)
154 {
155  id3->version = id3v2_version;
156 
157  avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
158  avio_w8(pb, 0);
159  avio_w8(pb, 0); /* flags */
160 
161  /* reserve space for size */
162  id3->size_pos = avio_tell(pb);
163  avio_wb32(pb, 0);
164 }
165 
166 static int write_metadata(AVIOContext *pb, AVDictionary **metadata,
167  ID3v2EncContext *id3, int enc)
168 {
169  AVDictionaryEntry *t = NULL;
170  int ret;
171 
173  if (id3->version == 3)
174  id3v2_3_metadata_split_date(metadata);
175  else if (id3->version == 4)
177 
178  while ((t = av_dict_get(*metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
179  if ((ret = id3v2_check_write_tag(id3, pb, t, ff_id3v2_tags, enc)) > 0) {
180  id3->len += ret;
181  continue;
182  }
183  if ((ret = id3v2_check_write_tag(id3, pb, t, id3->version == 3 ?
184  ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
185  id3->len += ret;
186  continue;
187  }
188 
189  /* unknown tag, write as TXXX frame */
190  if ((ret = id3v2_put_ttag(id3, pb, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
191  return ret;
192  id3->len += ret;
193  }
194 
195  return 0;
196 }
197 
198 static int write_chapter(AVFormatContext *s, ID3v2EncContext *id3, int id, int enc)
199 {
200  const AVRational time_base = {1, 1000};
201  AVChapter *ch = s->chapters[id];
202  uint8_t *dyn_buf = NULL;
203  AVIOContext *dyn_bc = NULL;
204  char name[123];
205  int len, start, end, ret;
206 
207  if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
208  goto fail;
209 
210  start = av_rescale_q(ch->start, ch->time_base, time_base);
211  end = av_rescale_q(ch->end, ch->time_base, time_base);
212 
213  snprintf(name, 122, "ch%d", id);
214  id3->len += avio_put_str(dyn_bc, name);
215  avio_wb32(dyn_bc, start);
216  avio_wb32(dyn_bc, end);
217  avio_wb32(dyn_bc, 0xFFFFFFFFu);
218  avio_wb32(dyn_bc, 0xFFFFFFFFu);
219 
220  if ((ret = write_metadata(dyn_bc, &ch->metadata, id3, enc)) < 0)
221  goto fail;
222 
223  len = avio_close_dyn_buf(dyn_bc, &dyn_buf);
224  id3->len += 16 + ID3v2_HEADER_SIZE;
225 
226  avio_wb32(s->pb, MKBETAG('C', 'H', 'A', 'P'));
227  avio_wb32(s->pb, len);
228  avio_wb16(s->pb, 0);
229  avio_write(s->pb, dyn_buf, len);
230 
231 fail:
232  if (dyn_bc && !dyn_buf)
233  avio_close_dyn_buf(dyn_bc, &dyn_buf);
234  av_freep(&dyn_buf);
235 
236  return ret;
237 }
238 
240 {
241  int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
243  int i, ret;
244 
245  if ((ret = write_metadata(s->pb, &s->metadata, id3, enc)) < 0)
246  return ret;
247 
248  for (i = 0; i < s->nb_chapters; i++) {
249  if ((ret = write_chapter(s, id3, i, enc)) < 0)
250  return ret;
251  }
252 
253  return 0;
254 }
255 
257 {
258  AVStream *st = s->streams[pkt->stream_index];
260 
261  AVIOContext *dyn_buf;
262  uint8_t *buf;
263  const CodecMime *mime = ff_id3v2_mime_tags;
264  const char *mimetype = NULL, *desc = "";
265  int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
267  int i, len, type = 0;
268 
269  /* get the mimetype*/
270  while (mime->id != AV_CODEC_ID_NONE) {
271  if (mime->id == st->codec->codec_id) {
272  mimetype = mime->str;
273  break;
274  }
275  mime++;
276  }
277  if (!mimetype) {
278  av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
279  "write an attached picture.\n", st->index);
280  return AVERROR(EINVAL);
281  }
282 
283  /* get the picture type */
284  e = av_dict_get(st->metadata, "comment", NULL, 0);
285  for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
287  type = i;
288  break;
289  }
290  }
291 
292  /* get the description */
293  if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
294  desc = e->value;
295 
296  /* use UTF16 only for non-ASCII strings */
297  if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(desc))
299 
300  /* start writing */
301  if (avio_open_dyn_buf(&dyn_buf) < 0)
302  return AVERROR(ENOMEM);
303 
304  avio_w8(dyn_buf, enc);
305  avio_put_str(dyn_buf, mimetype);
306  avio_w8(dyn_buf, type);
307  id3v2_encode_string(dyn_buf, desc, enc);
308  avio_write(dyn_buf, pkt->data, pkt->size);
309  len = avio_close_dyn_buf(dyn_buf, &buf);
310 
311  avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
312  if (id3->version == 3)
313  avio_wb32(s->pb, len);
314  else
315  id3v2_put_size(s->pb, len);
316  avio_wb16(s->pb, 0);
317  avio_write(s->pb, buf, len);
318  av_freep(&buf);
319 
320  id3->len += len + ID3v2_HEADER_SIZE;
321 
322  return 0;
323 }
324 
326  int padding_bytes)
327 {
328  int64_t cur_pos;
329 
330  if (padding_bytes < 0)
331  padding_bytes = 10;
332 
333  /* The ID3v2.3 specification states that 28 bits are used to represent the
334  * size of the whole tag. Therefore the current size of the tag needs to be
335  * subtracted from the upper limit of 2^28-1 to clip the value correctly. */
336  /* The minimum of 10 is an arbitrary amount of padding at the end of the tag
337  * to fix cover art display with some software such as iTunes, Traktor,
338  * Serato, Torq. */
339  padding_bytes = av_clip(padding_bytes, 10, 268435455 - id3->len);
340  ffio_fill(pb, 0, padding_bytes);
341  id3->len += padding_bytes;
342 
343  cur_pos = avio_tell(pb);
344  avio_seek(pb, id3->size_pos, SEEK_SET);
345  id3v2_put_size(pb, id3->len);
346  avio_seek(pb, cur_pos, SEEK_SET);
347 }
348 
349 int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
350  const char *magic)
351 {
352  ID3v2EncContext id3 = { 0 };
353  int ret;
354 
355  ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
356  if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
357  return ret;
359 
360  return 0;
361 }
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1471
#define NULL
Definition: coverity.c:32
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:416
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
Buffered I/O operations.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1125
int len
size of the tag written so far
Definition: id3v2.h:52
enum AVCodecID id
Definition: mxfenc.c:99
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition: id3v2enc.c:239
int index
stream index in AVFormatContext
Definition: avformat.h:843
int size
Definition: avcodec.h:1163
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:203
const char ff_id3v2_4_tags[][4]
ID3v2.4-only text information frames.
Definition: id3v2.c:94
#define FF_ARRAY_ELEMS(a)
static AVPacket pkt
AVDictionary * metadata
Definition: avformat.h:1239
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1113
Format I/O context.
Definition: avformat.h:1272
ID3v2Encoding
Definition: id3v2.h:42
char str[32]
Definition: internal.h:46
Public dictionary API.
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition: id3v2enc.c:152
#define ID3v2_HEADER_SIZE
Definition: id3v2.h:30
const AVMetadataConv ff_id3v2_34_metadata_conv[]
Definition: id3v2.c:43
if()
Definition: avfilter.c:975
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
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:85
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1340
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:39
uint8_t * data
Definition: avcodec.h:1162
uint32_t tag
Definition: movenc.c:1333
enum AVCodecID id
Definition: internal.h:47
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:365
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:177
#define av_log(a,...)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1482
const AVMetadataConv ff_id3v2_4_metadata_conv[]
Definition: id3v2.c:62
#define AVERROR(e)
Definition: error.h:43
static const struct endianess table[]
static int string_is_ascii(const uint8_t *str)
Definition: id3v2enc.c:40
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:199
AVChapter ** chapters
Definition: avformat.h:1472
static int write_chapter(AVFormatContext *s, ID3v2EncContext *id3, int id, int enc)
Definition: id3v2enc.c:198
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
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:105
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:129
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:163
int64_t size_pos
offset of the tag total size
Definition: id3v2.h:51
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
ret
Definition: avfilter.c:974
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
AVDictionary * metadata
Definition: avformat.h:916
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes)
Finalize an opened ID3v2 tag.
Definition: id3v2enc.c:325
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:334
int metadata_header_padding
Number of bytes to be written as padding in a metadata header.
Definition: avformat.h:1742
Stream structure.
Definition: avformat.h:842
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1238
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
enum AVCodecID codec_id
Definition: avcodec.h:1258
AVIOContext * pb
I/O context.
Definition: avformat.h:1314
static int id3v2_check_write_tag(ID3v2EncContext *id3, AVIOContext *pb, AVDictionaryEntry *t, const char table[][4], enum ID3v2Encoding enc)
Definition: id3v2enc.c:99
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:155
void * buf
Definition: avisynth_c.h:553
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:69
rational number numerator/denominator
Definition: rational.h:43
static int write_metadata(AVIOContext *pb, AVDictionary **metadata, ID3v2EncContext *id3, int enc)
Definition: id3v2enc.c:166
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str, enum ID3v2Encoding enc)
Definition: id3v2enc.c:46
#define snprintf
Definition: snprintf.h:34
int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version, const char *magic)
Write an ID3v2 tag containing all global metadata from s.
Definition: id3v2enc.c:349
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:422
int64_t start
Definition: avformat.h:1238
static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2, uint32_t tag, enum ID3v2Encoding enc)
Write a text frame with one (normal frames) or two (TXXX frames) strings according to encoding (only ...
Definition: id3v2enc.c:65
Main libavformat public API header.
static void id3v2_put_size(AVIOContext *pb, int size)
Definition: id3v2enc.c:32
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1237
char * key
Definition: dict.h:87
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
#define MKBETAG(a, b, c, d)
Definition: common.h:316
const char ff_id3v2_3_tags[][4]
ID3v2.3-only text information frames.
Definition: id3v2.c:100
char * value
Definition: dict.h:88
int len
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition: id3v2enc.c:256
static void id3v2_3_metadata_split_date(AVDictionary **pm)
Definition: id3v2enc.c:114
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:326
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:72
int stream_index
Definition: avcodec.h:1164
int version
ID3v2 minor version, either 3 or 4.
Definition: id3v2.h:50
This structure stores compressed data.
Definition: avcodec.h:1139
const char * name
Definition: opengl_enc.c:103