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.

258 lines
7.2KB

  1. /*
  2. * MP3 muxer
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <strings.h>
  22. #include "avformat.h"
  23. #include "id3v1.h"
  24. #include "id3v2.h"
  25. #include "rawenc.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/dict.h"
  30. static int id3v1_set_string(AVFormatContext *s, const char *key,
  31. uint8_t *buf, int buf_size)
  32. {
  33. AVDictionaryEntry *tag;
  34. if ((tag = av_dict_get(s->metadata, key, NULL, 0)))
  35. av_strlcpy(buf, tag->value, buf_size);
  36. return !!tag;
  37. }
  38. static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
  39. {
  40. AVDictionaryEntry *tag;
  41. int i, count = 0;
  42. memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
  43. buf[0] = 'T';
  44. buf[1] = 'A';
  45. buf[2] = 'G';
  46. count += id3v1_set_string(s, "TIT2", buf + 3, 30); //title
  47. count += id3v1_set_string(s, "TPE1", buf + 33, 30); //author|artist
  48. count += id3v1_set_string(s, "TALB", buf + 63, 30); //album
  49. count += id3v1_set_string(s, "TDRL", buf + 93, 4); //date
  50. count += id3v1_set_string(s, "comment", buf + 97, 30);
  51. if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
  52. buf[125] = 0;
  53. buf[126] = atoi(tag->value);
  54. count++;
  55. }
  56. buf[127] = 0xFF; /* default to unknown genre */
  57. if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) { //genre
  58. for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
  59. if (!strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
  60. buf[127] = i;
  61. count++;
  62. break;
  63. }
  64. }
  65. }
  66. return count;
  67. }
  68. /* simple formats */
  69. static void id3v2_put_size(AVFormatContext *s, int size)
  70. {
  71. avio_w8(s->pb, size >> 21 & 0x7f);
  72. avio_w8(s->pb, size >> 14 & 0x7f);
  73. avio_w8(s->pb, size >> 7 & 0x7f);
  74. avio_w8(s->pb, size & 0x7f);
  75. }
  76. static int string_is_ascii(const uint8_t *str)
  77. {
  78. while (*str && *str < 128) str++;
  79. return !*str;
  80. }
  81. /**
  82. * Write a text frame with one (normal frames) or two (TXXX frames) strings
  83. * according to encoding (only UTF-8 or UTF-16+BOM supported).
  84. * @return number of bytes written or a negative error code.
  85. */
  86. static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
  87. uint32_t tag, enum ID3v2Encoding enc)
  88. {
  89. int len;
  90. uint8_t *pb;
  91. int (*put)(AVIOContext*, const char*);
  92. AVIOContext *dyn_buf;
  93. if (avio_open_dyn_buf(&dyn_buf) < 0)
  94. return AVERROR(ENOMEM);
  95. /* check if the strings are ASCII-only and use UTF16 only if
  96. * they're not */
  97. if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
  98. (!str2 || string_is_ascii(str2)))
  99. enc = ID3v2_ENCODING_ISO8859;
  100. avio_w8(dyn_buf, enc);
  101. if (enc == ID3v2_ENCODING_UTF16BOM) {
  102. avio_wl16(dyn_buf, 0xFEFF); /* BOM */
  103. put = avio_put_str16le;
  104. } else
  105. put = avio_put_str;
  106. put(dyn_buf, str1);
  107. if (str2)
  108. put(dyn_buf, str2);
  109. len = avio_close_dyn_buf(dyn_buf, &pb);
  110. avio_wb32(s->pb, tag);
  111. id3v2_put_size(s, len);
  112. avio_wb16(s->pb, 0);
  113. avio_write(s->pb, pb, len);
  114. av_freep(&pb);
  115. return len + ID3v2_HEADER_SIZE;
  116. }
  117. static int mp3_write_trailer(struct AVFormatContext *s)
  118. {
  119. uint8_t buf[ID3v1_TAG_SIZE];
  120. /* write the id3v1 tag */
  121. if (id3v1_create_tag(s, buf) > 0) {
  122. avio_write(s->pb, buf, ID3v1_TAG_SIZE);
  123. avio_flush(s->pb);
  124. }
  125. return 0;
  126. }
  127. #if CONFIG_MP2_MUXER
  128. AVOutputFormat ff_mp2_muxer = {
  129. "mp2",
  130. NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
  131. "audio/x-mpeg",
  132. "mp2,m2a",
  133. 0,
  134. CODEC_ID_MP2,
  135. CODEC_ID_NONE,
  136. NULL,
  137. ff_raw_write_packet,
  138. mp3_write_trailer,
  139. };
  140. #endif
  141. #if CONFIG_MP3_MUXER
  142. typedef struct MP3Context {
  143. const AVClass *class;
  144. int id3v2_version;
  145. } MP3Context;
  146. static const AVOption options[] = {
  147. { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
  148. offsetof(MP3Context, id3v2_version), FF_OPT_TYPE_INT, {.dbl = 4}, 3, 4, AV_OPT_FLAG_ENCODING_PARAM},
  149. { NULL },
  150. };
  151. static const AVClass mp3_muxer_class = {
  152. .class_name = "MP3 muxer",
  153. .item_name = av_default_item_name,
  154. .option = options,
  155. .version = LIBAVUTIL_VERSION_INT,
  156. };
  157. static int id3v2_check_write_tag(AVFormatContext *s, AVDictionaryEntry *t, const char table[][4],
  158. enum ID3v2Encoding enc)
  159. {
  160. uint32_t tag;
  161. int i;
  162. if (t->key[0] != 'T' || strlen(t->key) != 4)
  163. return -1;
  164. tag = AV_RB32(t->key);
  165. for (i = 0; *table[i]; i++)
  166. if (tag == AV_RB32(table[i]))
  167. return id3v2_put_ttag(s, t->value, NULL, tag, enc);
  168. return -1;
  169. }
  170. /**
  171. * Write an ID3v2 header at beginning of stream
  172. */
  173. static int mp3_write_header(struct AVFormatContext *s)
  174. {
  175. MP3Context *mp3 = s->priv_data;
  176. AVDictionaryEntry *t = NULL;
  177. int totlen = 0, enc = mp3->id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
  178. ID3v2_ENCODING_UTF8;
  179. int64_t size_pos, cur_pos;
  180. avio_wb32(s->pb, MKBETAG('I', 'D', '3', mp3->id3v2_version));
  181. avio_w8(s->pb, 0);
  182. avio_w8(s->pb, 0); /* flags */
  183. /* reserve space for size */
  184. size_pos = avio_tell(s->pb);
  185. avio_wb32(s->pb, 0);
  186. ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
  187. if (mp3->id3v2_version == 4)
  188. ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
  189. while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
  190. int ret;
  191. if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
  192. totlen += ret;
  193. continue;
  194. }
  195. if ((ret = id3v2_check_write_tag(s, t, mp3->id3v2_version == 3 ?
  196. ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
  197. totlen += ret;
  198. continue;
  199. }
  200. /* unknown tag, write as TXXX frame */
  201. if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
  202. return ret;
  203. totlen += ret;
  204. }
  205. cur_pos = avio_tell(s->pb);
  206. avio_seek(s->pb, size_pos, SEEK_SET);
  207. id3v2_put_size(s, totlen);
  208. avio_seek(s->pb, cur_pos, SEEK_SET);
  209. return 0;
  210. }
  211. AVOutputFormat ff_mp3_muxer = {
  212. "mp3",
  213. NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
  214. "audio/x-mpeg",
  215. "mp3",
  216. sizeof(MP3Context),
  217. CODEC_ID_MP3,
  218. CODEC_ID_NONE,
  219. mp3_write_header,
  220. ff_raw_write_packet,
  221. mp3_write_trailer,
  222. AVFMT_NOTIMESTAMPS,
  223. .priv_class = &mp3_muxer_class,
  224. };
  225. #endif