id3v2enc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <strings.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(AVFormatContext *s, int size)
  29. {
  30. avio_w8(s->pb, size >> 21 & 0x7f);
  31. avio_w8(s->pb, size >> 14 & 0x7f);
  32. avio_w8(s->pb, size >> 7 & 0x7f);
  33. avio_w8(s->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. /**
  41. * Write a text frame with one (normal frames) or two (TXXX frames) strings
  42. * according to encoding (only UTF-8 or UTF-16+BOM supported).
  43. * @return number of bytes written or a negative error code.
  44. */
  45. static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
  46. uint32_t tag, enum ID3v2Encoding enc)
  47. {
  48. int len;
  49. uint8_t *pb;
  50. int (*put)(AVIOContext*, const char*);
  51. AVIOContext *dyn_buf;
  52. if (avio_open_dyn_buf(&dyn_buf) < 0)
  53. return AVERROR(ENOMEM);
  54. /* check if the strings are ASCII-only and use UTF16 only if
  55. * they're not */
  56. if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
  57. (!str2 || string_is_ascii(str2)))
  58. enc = ID3v2_ENCODING_ISO8859;
  59. avio_w8(dyn_buf, enc);
  60. if (enc == ID3v2_ENCODING_UTF16BOM) {
  61. avio_wl16(dyn_buf, 0xFEFF); /* BOM */
  62. put = avio_put_str16le;
  63. } else
  64. put = avio_put_str;
  65. put(dyn_buf, str1);
  66. if (str2)
  67. put(dyn_buf, str2);
  68. len = avio_close_dyn_buf(dyn_buf, &pb);
  69. avio_wb32(s->pb, tag);
  70. id3v2_put_size(s, len);
  71. avio_wb16(s->pb, 0);
  72. avio_write(s->pb, pb, len);
  73. av_freep(&pb);
  74. return len + ID3v2_HEADER_SIZE;
  75. }
  76. static int id3v2_check_write_tag(AVFormatContext *s, AVDictionaryEntry *t, const char table[][4],
  77. enum ID3v2Encoding enc)
  78. {
  79. uint32_t tag;
  80. int i;
  81. if (t->key[0] != 'T' || strlen(t->key) != 4)
  82. return -1;
  83. tag = AV_RB32(t->key);
  84. for (i = 0; *table[i]; i++)
  85. if (tag == AV_RB32(table[i]))
  86. return id3v2_put_ttag(s, t->value, NULL, tag, enc);
  87. return -1;
  88. }
  89. static void id3v2_3_metadata_split_date(AVDictionary **pm)
  90. {
  91. AVDictionaryEntry *mtag = NULL;
  92. AVDictionary *dst = NULL;
  93. const char *key, *value;
  94. char year[5] = {0}, day_month[5] = {0};
  95. int i;
  96. while ((mtag = av_dict_get(*pm, "", mtag, AV_DICT_IGNORE_SUFFIX))) {
  97. key = mtag->key;
  98. if (!strcasecmp(key, "date")) {
  99. /* split date tag using "YYYY-MM-DD" format into year and month/day segments */
  100. value = mtag->value;
  101. i = 0;
  102. while (value[i] >= '0' && value[i] <= '9') i++;
  103. if (value[i] == '\0' || value[i] == '-') {
  104. av_strlcpy(year, value, sizeof(year));
  105. av_dict_set(&dst, "TYER", year, 0);
  106. if (value[i] == '-' &&
  107. value[i+1] >= '0' && value[i+1] <= '1' &&
  108. value[i+2] >= '0' && value[i+2] <= '9' &&
  109. value[i+3] == '-' &&
  110. value[i+4] >= '0' && value[i+4] <= '3' &&
  111. value[i+5] >= '0' && value[i+5] <= '9' &&
  112. (value[i+6] == '\0' || value[i+6] == ' ')) {
  113. snprintf(day_month, sizeof(day_month), "%.2s%.2s", value + i + 4, value + i + 1);
  114. av_dict_set(&dst, "TDAT", day_month, 0);
  115. }
  116. } else
  117. av_dict_set(&dst, key, value, 0);
  118. } else
  119. av_dict_set(&dst, key, mtag->value, 0);
  120. }
  121. av_dict_free(pm);
  122. *pm = dst;
  123. }
  124. int ff_id3v2_write(struct AVFormatContext *s, int id3v2_version,
  125. const char *magic)
  126. {
  127. int64_t size_pos, cur_pos;
  128. AVDictionaryEntry *t = NULL;
  129. int totlen = 0, enc = id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
  130. ID3v2_ENCODING_UTF8;
  131. avio_wb32(s->pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
  132. avio_w8(s->pb, 0);
  133. avio_w8(s->pb, 0); /* flags */
  134. /* reserve space for size */
  135. size_pos = avio_tell(s->pb);
  136. avio_wb32(s->pb, 0);
  137. ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
  138. if (id3v2_version == 3)
  139. id3v2_3_metadata_split_date(&s->metadata);
  140. else if (id3v2_version == 4)
  141. ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
  142. while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
  143. int ret;
  144. if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
  145. totlen += ret;
  146. continue;
  147. }
  148. if ((ret = id3v2_check_write_tag(s, t, id3v2_version == 3 ?
  149. ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
  150. totlen += ret;
  151. continue;
  152. }
  153. /* unknown tag, write as TXXX frame */
  154. if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
  155. return ret;
  156. totlen += ret;
  157. }
  158. cur_pos = avio_tell(s->pb);
  159. avio_seek(s->pb, size_pos, SEEK_SET);
  160. id3v2_put_size(s, totlen);
  161. avio_seek(s->pb, cur_pos, SEEK_SET);
  162. return 0;
  163. }