4 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/avstring.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/intreadwrite.h"
29 #include "avio_internal.h"
32 static void id3v2_put_size(AVIOContext *pb, int size)
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);
40 static int string_is_ascii(const uint8_t *str)
42 while (*str && *str < 128) str++;
46 static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
47 enum ID3v2Encoding enc)
49 int (*put)(AVIOContext*, const char*);
51 if (enc == ID3v2_ENCODING_UTF16BOM) {
52 avio_wl16(pb, 0xFEFF); /* BOM */
53 put = avio_put_str16le;
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.
65 static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
66 uint32_t tag, enum ID3v2Encoding enc)
71 if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
74 /* check if the strings are ASCII-only and use UTF16 only if
76 if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
77 (!str2 || string_is_ascii(str2)))
78 enc = ID3v2_ENCODING_ISO8859;
80 avio_w8(dyn_buf, enc);
81 id3v2_encode_string(dyn_buf, str1, enc);
83 id3v2_encode_string(dyn_buf, str2, enc);
84 len = avio_get_dyn_buf(dyn_buf, &pb);
86 avio_wb32(avioc, tag);
87 /* ID3v2.3 frame size is not sync-safe */
88 if (id3->version == 3)
89 avio_wb32(avioc, len);
91 id3v2_put_size(avioc, len);
93 avio_write(avioc, pb, len);
95 ffio_free_dyn_buf(&dyn_buf);
96 return len + ID3v2_HEADER_SIZE;
100 * Write a priv frame with owner and data. 'key' is the owner prepended with
101 * ID3v2_PRIV_METADATA_PREFIX. 'data' is provided as a string. Any \xXX
102 * (where 'X' is a valid hex digit) will be unescaped to the byte value.
104 static int id3v2_put_priv(ID3v2EncContext *id3, AVIOContext *avioc, const char *key, const char *data)
108 AVIOContext *dyn_buf;
110 if (!av_strstart(key, ID3v2_PRIV_METADATA_PREFIX, &key)) {
114 if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
117 // owner + null byte.
118 avio_write(dyn_buf, key, strlen(key) + 1);
121 if (av_strstart(data, "\\x", &data)) {
122 if (data[0] && data[1] && av_isxdigit(data[0]) && av_isxdigit(data[1])) {
123 char digits[] = {data[0], data[1], 0};
124 avio_w8(dyn_buf, strtol(digits, NULL, 16));
127 ffio_free_dyn_buf(&dyn_buf);
128 av_log(avioc, AV_LOG_ERROR, "Invalid escape '\\x%.2s' in metadata tag '"
129 ID3v2_PRIV_METADATA_PREFIX "%s'.\n", data, key);
130 return AVERROR(EINVAL);
133 avio_write(dyn_buf, data++, 1);
137 len = avio_get_dyn_buf(dyn_buf, &pb);
139 avio_wb32(avioc, MKBETAG('P', 'R', 'I', 'V'));
140 if (id3->version == 3)
141 avio_wb32(avioc, len);
143 id3v2_put_size(avioc, len);
145 avio_write(avioc, pb, len);
147 ffio_free_dyn_buf(&dyn_buf);
149 return len + ID3v2_HEADER_SIZE;
152 static int id3v2_check_write_tag(ID3v2EncContext *id3, AVIOContext *pb, AVDictionaryEntry *t,
153 const char table[][4], enum ID3v2Encoding enc)
158 if (t->key[0] != 'T' || strlen(t->key) != 4)
160 tag = AV_RB32(t->key);
161 for (i = 0; *table[i]; i++)
162 if (tag == AV_RB32(table[i]))
163 return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
167 static void id3v2_3_metadata_split_date(AVDictionary **pm)
169 AVDictionaryEntry *mtag = NULL;
170 AVDictionary *dst = NULL;
171 const char *key, *value;
172 char year[5] = {0}, day_month[5] = {0};
175 while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
177 if (!av_strcasecmp(key, "date")) {
178 /* split date tag using "YYYY-MM-DD" format into year and month/day segments */
181 while (value[i] >= '0' && value[i] <= '9') i++;
182 if (value[i] == '\0' || value[i] == '-') {
183 av_strlcpy(year, value, sizeof(year));
184 av_dict_set(&dst, "TYER", year, 0);
186 if (value[i] == '-' &&
187 value[i+1] >= '0' && value[i+1] <= '1' &&
188 value[i+2] >= '0' && value[i+2] <= '9' &&
190 value[i+4] >= '0' && value[i+4] <= '3' &&
191 value[i+5] >= '0' && value[i+5] <= '9' &&
192 (value[i+6] == '\0' || value[i+6] == ' ')) {
193 snprintf(day_month, sizeof(day_month), "%.2s%.2s", value + i + 4, value + i + 1);
194 av_dict_set(&dst, "TDAT", day_month, 0);
197 av_dict_set(&dst, key, value, 0);
199 av_dict_set(&dst, key, mtag->value, 0);
205 void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
208 id3->version = id3v2_version;
210 avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
212 avio_w8(pb, 0); /* flags */
214 /* reserve space for size */
215 id3->size_pos = avio_tell(pb);
219 static int write_metadata(AVIOContext *pb, AVDictionary **metadata,
220 ID3v2EncContext *id3, int enc)
222 AVDictionaryEntry *t = NULL;
225 ff_metadata_conv(metadata, ff_id3v2_34_metadata_conv, NULL);
226 if (id3->version == 3)
227 id3v2_3_metadata_split_date(metadata);
228 else if (id3->version == 4)
229 ff_metadata_conv(metadata, ff_id3v2_4_metadata_conv, NULL);
231 while ((t = av_dict_get(*metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
232 if ((ret = id3v2_check_write_tag(id3, pb, t, ff_id3v2_tags, enc)) > 0) {
236 if ((ret = id3v2_check_write_tag(id3, pb, t, id3->version == 3 ?
237 ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
242 if ((ret = id3v2_put_priv(id3, pb, t->key, t->value)) > 0) {
245 } else if (ret < 0) {
249 /* unknown tag, write as TXXX frame */
250 if ((ret = id3v2_put_ttag(id3, pb, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
258 static int write_ctoc(AVFormatContext *s, ID3v2EncContext *id3, int enc)
265 if (s->nb_chapters == 0)
268 if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
271 avio_put_str(dyn_bc, "toc");
272 avio_w8(dyn_bc, 0x03);
273 avio_w8(dyn_bc, s->nb_chapters);
274 for (int i = 0; i < s->nb_chapters; i++) {
275 snprintf(name, 122, "ch%d", i);
276 avio_put_str(dyn_bc, name);
278 len = avio_get_dyn_buf(dyn_bc, &dyn_buf);
279 id3->len += len + ID3v2_HEADER_SIZE;
281 avio_wb32(s->pb, MKBETAG('C', 'T', 'O', 'C'));
282 avio_wb32(s->pb, len);
284 avio_write(s->pb, dyn_buf, len);
286 ffio_free_dyn_buf(&dyn_bc);
291 static int write_chapter(AVFormatContext *s, ID3v2EncContext *id3, int id, int enc)
293 const AVRational time_base = {1, 1000};
294 AVChapter *ch = s->chapters[id];
298 int len, start, end, ret;
300 if ((ret = avio_open_dyn_buf(&dyn_bc)) < 0)
303 start = av_rescale_q(ch->start, ch->time_base, time_base);
304 end = av_rescale_q(ch->end, ch->time_base, time_base);
306 snprintf(name, 122, "ch%d", id);
307 id3->len += avio_put_str(dyn_bc, name);
308 avio_wb32(dyn_bc, start);
309 avio_wb32(dyn_bc, end);
310 avio_wb32(dyn_bc, 0xFFFFFFFFu);
311 avio_wb32(dyn_bc, 0xFFFFFFFFu);
313 if ((ret = write_metadata(dyn_bc, &ch->metadata, id3, enc)) < 0)
316 len = avio_get_dyn_buf(dyn_bc, &dyn_buf);
317 id3->len += 16 + ID3v2_HEADER_SIZE;
319 avio_wb32(s->pb, MKBETAG('C', 'H', 'A', 'P'));
320 avio_wb32(s->pb, len);
322 avio_write(s->pb, dyn_buf, len);
325 ffio_free_dyn_buf(&dyn_bc);
330 int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
332 int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
336 ff_standardize_creation_time(s);
337 if ((ret = write_metadata(s->pb, &s->metadata, id3, enc)) < 0)
340 if ((ret = write_ctoc(s, id3, enc)) < 0)
343 for (i = 0; i < s->nb_chapters; i++) {
344 if ((ret = write_chapter(s, id3, i, enc)) < 0)
351 int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
353 AVStream *st = s->streams[pkt->stream_index];
354 AVDictionaryEntry *e;
356 AVIOContext *dyn_buf;
358 const CodecMime *mime = ff_id3v2_mime_tags;
359 const char *mimetype = NULL, *desc = "";
360 int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
362 int i, len, type = 0, ret;
364 /* get the mimetype*/
365 while (mime->id != AV_CODEC_ID_NONE) {
366 if (mime->id == st->codecpar->codec_id) {
367 mimetype = mime->str;
373 av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
374 "write an attached picture.\n", st->index);
375 return AVERROR(EINVAL);
378 /* get the picture type */
379 e = av_dict_get(st->metadata, "comment", NULL, 0);
380 for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
381 if (!av_strcasecmp(e->value, ff_id3v2_picture_types[i])) {
387 /* get the description */
388 if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
391 /* use UTF16 only for non-ASCII strings */
392 if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(desc))
393 enc = ID3v2_ENCODING_ISO8859;
396 if ((ret = avio_open_dyn_buf(&dyn_buf)) < 0)
399 avio_w8(dyn_buf, enc);
400 avio_put_str(dyn_buf, mimetype);
401 avio_w8(dyn_buf, type);
402 id3v2_encode_string(dyn_buf, desc, enc);
403 avio_write(dyn_buf, pkt->data, pkt->size);
404 len = avio_get_dyn_buf(dyn_buf, &buf);
406 avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
407 if (id3->version == 3)
408 avio_wb32(s->pb, len);
410 id3v2_put_size(s->pb, len);
412 avio_write(s->pb, buf, len);
413 ffio_free_dyn_buf(&dyn_buf);
415 id3->len += len + ID3v2_HEADER_SIZE;
420 void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb,
425 if (padding_bytes < 0)
428 /* The ID3v2.3 specification states that 28 bits are used to represent the
429 * size of the whole tag. Therefore the current size of the tag needs to be
430 * subtracted from the upper limit of 2^28-1 to clip the value correctly. */
431 /* The minimum of 10 is an arbitrary amount of padding at the end of the tag
432 * to fix cover art display with some software such as iTunes, Traktor,
434 padding_bytes = av_clip(padding_bytes, 10, 268435455 - id3->len);
435 ffio_fill(pb, 0, padding_bytes);
436 id3->len += padding_bytes;
438 cur_pos = avio_tell(pb);
439 avio_seek(pb, id3->size_pos, SEEK_SET);
440 id3v2_put_size(pb, id3->len);
441 avio_seek(pb, cur_pos, SEEK_SET);
444 int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
447 ID3v2EncContext id3 = { 0 };
450 ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
451 if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
453 ff_id3v2_finish(&id3, s->pb, s->metadata_header_padding);