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.

356 lines
10KB

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