id3v2enc.c 5.9 KB

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