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.

244 lines
7.0KB

  1. /*
  2. * ID3v2 header writer
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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/dict.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "avformat.h"
  25. #include "avio.h"
  26. #include "id3v2.h"
  27. static void id3v2_put_size(AVIOContext *pb, int size)
  28. {
  29. avio_w8(pb, size >> 21 & 0x7f);
  30. avio_w8(pb, size >> 14 & 0x7f);
  31. avio_w8(pb, size >> 7 & 0x7f);
  32. avio_w8(pb, size & 0x7f);
  33. }
  34. static int string_is_ascii(const uint8_t *str)
  35. {
  36. while (*str && *str < 128) str++;
  37. return !*str;
  38. }
  39. static void id3v2_encode_string(AVIOContext *pb, const uint8_t *str,
  40. enum ID3v2Encoding enc)
  41. {
  42. int (*put)(AVIOContext*, const char*);
  43. if (enc == ID3v2_ENCODING_UTF16BOM) {
  44. avio_wl16(pb, 0xFEFF); /* BOM */
  45. put = avio_put_str16le;
  46. } else
  47. put = avio_put_str;
  48. put(pb, str);
  49. }
  50. /**
  51. * Write a text frame with one (normal frames) or two (TXXX frames) strings
  52. * according to encoding (only UTF-8 or UTF-16+BOM supported).
  53. * @return number of bytes written or a negative error code.
  54. */
  55. static int id3v2_put_ttag(ID3v2EncContext *id3, AVIOContext *avioc, const char *str1, const char *str2,
  56. uint32_t tag, enum ID3v2Encoding enc)
  57. {
  58. int len;
  59. uint8_t *pb;
  60. AVIOContext *dyn_buf;
  61. if (avio_open_dyn_buf(&dyn_buf) < 0)
  62. return AVERROR(ENOMEM);
  63. /* check if the strings are ASCII-only and use UTF16 only if
  64. * they're not */
  65. if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
  66. (!str2 || string_is_ascii(str2)))
  67. enc = ID3v2_ENCODING_ISO8859;
  68. avio_w8(dyn_buf, enc);
  69. id3v2_encode_string(dyn_buf, str1, enc);
  70. if (str2)
  71. id3v2_encode_string(dyn_buf, str2, enc);
  72. len = avio_close_dyn_buf(dyn_buf, &pb);
  73. avio_wb32(avioc, tag);
  74. /* ID3v2.3 frame size is not synchsafe */
  75. if (id3->version == 3)
  76. avio_wb32(avioc, len);
  77. else
  78. id3v2_put_size(avioc, len);
  79. avio_wb16(avioc, 0);
  80. avio_write(avioc, pb, len);
  81. av_freep(&pb);
  82. return len + ID3v2_HEADER_SIZE;
  83. }
  84. static int id3v2_check_write_tag(ID3v2EncContext *id3, AVIOContext *pb, AVDictionaryEntry *t,
  85. const char table[][4], enum ID3v2Encoding enc)
  86. {
  87. uint32_t tag;
  88. int i;
  89. if (t->key[0] != 'T' || strlen(t->key) != 4)
  90. return -1;
  91. tag = AV_RB32(t->key);
  92. for (i = 0; *table[i]; i++)
  93. if (tag == AV_RB32(table[i]))
  94. return id3v2_put_ttag(id3, pb, t->value, NULL, tag, enc);
  95. return -1;
  96. }
  97. void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version,
  98. const char *magic)
  99. {
  100. id3->version = id3v2_version;
  101. avio_wb32(pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
  102. avio_w8(pb, 0);
  103. avio_w8(pb, 0); /* flags */
  104. /* reserve space for size */
  105. id3->size_pos = avio_tell(pb);
  106. avio_wb32(pb, 0);
  107. }
  108. int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
  109. {
  110. AVDictionaryEntry *t = NULL;
  111. int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
  112. ID3v2_ENCODING_UTF8;
  113. ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
  114. if (id3->version == 4)
  115. ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
  116. while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
  117. int ret;
  118. if ((ret = id3v2_check_write_tag(id3, s->pb, t, ff_id3v2_tags, enc)) > 0) {
  119. id3->len += ret;
  120. continue;
  121. }
  122. if ((ret = id3v2_check_write_tag(id3, s->pb, t, id3->version == 3 ?
  123. ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
  124. id3->len += ret;
  125. continue;
  126. }
  127. /* unknown tag, write as TXXX frame */
  128. if ((ret = id3v2_put_ttag(id3, s->pb, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
  129. return ret;
  130. id3->len += ret;
  131. }
  132. return 0;
  133. }
  134. int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
  135. {
  136. AVStream *st = s->streams[pkt->stream_index];
  137. AVDictionaryEntry *e;
  138. AVIOContext *dyn_buf;
  139. uint8_t *buf;
  140. const CodecMime *mime = ff_id3v2_mime_tags;
  141. const char *mimetype = NULL, *desc = "";
  142. int enc = id3->version == 3 ? ID3v2_ENCODING_UTF16BOM :
  143. ID3v2_ENCODING_UTF8;
  144. int i, len, type = 0;
  145. /* get the mimetype*/
  146. while (mime->id != CODEC_ID_NONE) {
  147. if (mime->id == st->codec->codec_id) {
  148. mimetype = mime->str;
  149. break;
  150. }
  151. mime++;
  152. }
  153. if (!mimetype) {
  154. av_log(s, AV_LOG_ERROR, "No mimetype is known for stream %d, cannot "
  155. "write an attached picture.\n", st->index);
  156. return AVERROR(EINVAL);
  157. }
  158. /* get the picture type */
  159. e = av_dict_get(st->metadata, "comment", NULL, 0);
  160. for (i = 0; e && i < FF_ARRAY_ELEMS(ff_id3v2_picture_types); i++) {
  161. if (strstr(ff_id3v2_picture_types[i], e->value) == ff_id3v2_picture_types[i]) {
  162. type = i;
  163. break;
  164. }
  165. }
  166. /* get the description */
  167. if ((e = av_dict_get(st->metadata, "title", NULL, 0)))
  168. desc = e->value;
  169. /* start writing */
  170. if (avio_open_dyn_buf(&dyn_buf) < 0)
  171. return AVERROR(ENOMEM);
  172. avio_w8(dyn_buf, enc);
  173. avio_put_str(dyn_buf, mimetype);
  174. avio_w8(dyn_buf, type);
  175. id3v2_encode_string(dyn_buf, desc, enc);
  176. avio_write(dyn_buf, pkt->data, pkt->size);
  177. len = avio_close_dyn_buf(dyn_buf, &buf);
  178. avio_wb32(s->pb, MKBETAG('A', 'P', 'I', 'C'));
  179. if (id3->version == 3)
  180. avio_wb32(s->pb, len);
  181. else
  182. id3v2_put_size(s->pb, len);
  183. avio_wb16(s->pb, 0);
  184. avio_write(s->pb, buf, len);
  185. av_freep(&buf);
  186. id3->len += len + ID3v2_HEADER_SIZE;
  187. return 0;
  188. }
  189. void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb)
  190. {
  191. int64_t cur_pos = avio_tell(pb);
  192. avio_seek(pb, id3->size_pos, SEEK_SET);
  193. id3v2_put_size(pb, id3->len);
  194. avio_seek(pb, cur_pos, SEEK_SET);
  195. }
  196. int ff_id3v2_write_simple(struct AVFormatContext *s, int id3v2_version,
  197. const char *magic)
  198. {
  199. ID3v2EncContext id3 = { 0 };
  200. int ret;
  201. ff_id3v2_start(&id3, s->pb, id3v2_version, magic);
  202. if ((ret = ff_id3v2_write_metadata(s, &id3)) < 0)
  203. return ret;
  204. ff_id3v2_finish(&id3, s->pb);
  205. return 0;
  206. }