id3v2enc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "id3v2.h"
  28. static void id3v2_put_size(AVIOContext *pb, int size)
  29. {
  30. avio_w8(pb, size >> 21 & 0x7f);
  31. avio_w8(pb, size >> 14 & 0x7f);
  32. avio_w8(pb, size >> 7 & 0x7f);
  33. avio_w8(pb, size & 0x7f);
  34. }
  35. static int string_is_ascii(const uint8_t *str)
  36. {
  37. while (*str && *str < 128) str++;
  38. return !*str;
  39. }
  40. static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
  41. enum ID3v2Encoding enc)
  42. {
  43. int (*put)(AVIOContext*, const char*);
  44. if (enc == ID3v2_ENCODING_UTF16BOM) {
  45. avio_wl16(pb, 0xFEFF); /* BOM */
  46. put = avio_put_str16le;
  47. } else
  48. put = avio_put_str;
  49. put(pb, str);
  50. }
  51. /**
  52. * Write a text frame with one (normal frames) or two (TXXX frames) strings
  53. * according to encoding (only UTF-8 or UTF-16+BOM supported).
  54. * @return number of bytes written or a negative error code.
  55. */
  56. static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
  57. uint32_t tag, enum ID3v2Encoding enc)
  58. {
  59. int len;
  60. uint8_t *pb;
  61. AVIOContext *dyn_buf;
  62. if (avio_open_dyn_buf(&dyn_buf) < 0)
  63. return AVERROR(ENOMEM);
  64. /* check if the strings are ASCII-only and use UTF16 only if
  65. * they're not */
  66. if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
  67. (!str2 || string_is_ascii(str2)))
  68. enc = ID3v2_ENCODING_ISO8859;
  69. avio_w8(dyn_buf, enc);
  70. id3v2_encode_string(dyn_buf, str1, enc);
  71. if (str2)
  72. id3v2_encode_string(dyn_buf, str2, enc);
  73. len = avio_close_dyn_buf(dyn_buf, &pb);
  74. avio_wb32(avioc, tag);
  75. /* ID3v2.3 frame size is not synchsafe */
  76. if (id3->version == 3)
  77. avio_wb32(avioc, len);
  78. else
  79. id3v2_put_size(avioc, len);
  80. avio_wb16(avioc, 0);
  81. avio_write(avioc, pb, len);
  82. av_freep(&pb);
  83. return len + ID3v2_HEADER_SIZE;
  84. }
  85. static int id3v2_check_write_tag(ID3v2EncContext *id3, AVIOContext *pb, AVDictionaryEntry *t,
  86. const char table[][4], enum ID3v2Encoding enc)
  87. {
  88. uint32_t tag;
  89. int i;
  90. if (t->key[0] != 'T' || strlen(t->key) != 4)
  91. return -1;
  92. tag = AV_RB32(t->key);
  93. for (i = 0; *table[i]; i++)
  94. if (tag == AV_RB32(table[i]))
  95. return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
  96. return -1;
  97. }
  98. static void id3v2_3_metadata_split_date(AVDictionary **pm)
  99. {
  100. AVDictionaryEntry *mtag = NULL;
  101. AVDictionary *dst = NULL;
  102. const char *key, *value;
  103. char year[5] = {0}, day_month[5] = {0};
  104. int i;
  105. while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
  106. key = mtag->key;
  107. if (!av_strcasecmp(key, "date")) {
  108. /* split date tag using "YYYY-MM-DD" format into year and month/day segments */
  109. value = mtag->value;
  110. i = 0;
  111. while (value[i] >= '0' && value[i] <= '9') i++;
  112. if (value[i] == '\0' || value[i] == '-') {
  113. av_strlcpy(year, value, sizeof(year));
  114. av_dict_set(&dst, "TYER", year, 0);
  115. if (value[i] == '-' &&
  116. value[i+1] >= '0' && value[i+1] <= '1' &&
  117. value[i+2] >= '0' && value[i+2] <= '9' &&
  118. value[i+3] == '-' &&
  119. value[i+4] >= '0' && value[i+4] <= '3' &&
  120. value[i+5] >= '0' && value[i+5] <= '9' &&
  121. (value[i+6] == '\0' || value[i+6] == ' ')) {
  122. snprintf(day_month, sizeof(day_month), "%.2s%.2s", value + i + 4, value + i + 1);
  123. av_dict_set(&dst, "TDAT", day_month, 0);
  124. }
  125. } else
  126. av_dict_set(&dst, key, value, 0);
  127. } else
  128. av_dict_set(&dst, key, mtag->value, 0);
  129. }
  130. av_dict_free(pm);
  131. *pm = dst;
  132. }
  133. void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
  134. const char *magic)
  135. {
  136. id3->version = id3v2_version;
  137. avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
  138. avio_w8(pb, 0);
  139. avio_w8(pb, 0); /* flags */
  140. /* reserve space for size */
  141. id3->size_pos = avio_tell(pb);
  142. avio_wb32(pb, 0);
  143. }
  144. int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
  145. {
  146. AVDictionaryEntry *t = NULL;
  147. int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
  148. ID3v2_ENCODING_UTF8;
  149. ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
  150. if (id3->version == 3)
  151. id3v2_3_metadata_split_date(&s->metadata);
  152. else if (id3->version == 4)
  153. ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
  154. while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
  155. int ret;
  156. if ((ret = id3v2_check_write_tag(id3, s->pb, t, ff_id3v2_tags, enc)) > 0) {
  157. id3->len += ret;
  158. continue;
  159. }
  160. if ((ret = id3v2_check_write_tag(id3, s->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, s->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. int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
  173. {
  174. AVStream *st = s->streams[pkt->stream_index];
  175. AVDictionaryEntry *e;
  176. AVIOContext *dyn_buf;
  177. uint8_t *buf;
  178. const CodecMime *mime = ff_id3v2_mime_tags;
  179. const char *mimetype = NULL, *desc = "";
  180. int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
  181. ID3v2_ENCODING_UTF8;
  182. int i, len, type = 0;
  183. /* get the mimetype*/
  184. while (mime->id != AV_CODEC_ID_NONE) {
  185. if (mime->id == st->codec->codec_id) {
  186. mimetype = mime->str;
  187. break;
  188. }
  189. mime++;
  190. }
  191. if (!mimetype) {
  192. av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
  193. "write an attached picture.\n", st->index);
  194. return AVERROR(EINVAL);
  195. }
  196. /* get the picture type */
  197. e = av_dict_get(st->metadata, "comment", NULL, 0);
  198. for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
  199. if (strstr(ff_id3v2_picture_types[i], e->value) == ff_id3v2_picture_types[i]) {
  200. type = i;
  201. break;
  202. }
  203. }
  204. /* get the description */
  205. if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
  206. desc = e->value;
  207. /* start writing */
  208. if (avio_open_dyn_buf(&dyn_buf) < 0)
  209. return AVERROR(ENOMEM);
  210. avio_w8(dyn_buf, enc);
  211. avio_put_str(dyn_buf, mimetype);
  212. avio_w8(dyn_buf, type);
  213. id3v2_encode_string(dyn_buf, desc, enc);
  214. avio_write(dyn_buf, pkt->data, pkt->size);
  215. len = avio_close_dyn_buf(dyn_buf, &buf);
  216. avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
  217. if (id3->version == 3)
  218. avio_wb32(s->pb, len);
  219. else
  220. id3v2_put_size(s->pb, len);
  221. avio_wb16(s->pb, 0);
  222. avio_write(s->pb, buf, len);
  223. av_freep(&buf);
  224. id3->len += len + ID3v2_HEADER_SIZE;
  225. return 0;
  226. }
  227. void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb)
  228. {
  229. int64_t cur_pos = avio_tell(pb);
  230. avio_seek(pb, id3->size_pos, SEEK_SET);
  231. id3v2_put_size(pb, id3->len);
  232. avio_seek(pb, cur_pos, SEEK_SET);
  233. }
  234. int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
  235. const char *magic)
  236. {
  237. ID3v2EncContext id3 = { 0 };
  238. int ret;
  239. ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
  240. if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
  241. return ret;
  242. ff_id3v2_finish(&id3, s->pb);
  243. return 0;
  244. }