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.

257 lines
7.2KB

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