You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
11KB

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