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.

251 lines
6.9KB

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