mp3enc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. static int string_is_ascii(const uint8_t *str)
  74. {
  75. while (*str && *str < 128) str++;
  76. return !*str;
  77. }
  78. /**
  79. * Write a text frame with one (normal frames) or two (TXXX frames) strings
  80. * according to encoding (only UTF-8 or UTF-16+BOM supported).
  81. * @return number of bytes written or a negative error code.
  82. */
  83. static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
  84. uint32_t tag, enum ID3v2Encoding enc)
  85. {
  86. int len;
  87. uint8_t *pb;
  88. int (*put)(ByteIOContext*, const char*);
  89. ByteIOContext *dyn_buf;
  90. if (url_open_dyn_buf(&dyn_buf) < 0)
  91. return AVERROR(ENOMEM);
  92. /* check if the strings are ASCII-only and use UTF16 only if
  93. * they're not */
  94. if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
  95. (!str2 || string_is_ascii(str2)))
  96. enc = ID3v2_ENCODING_ISO8859;
  97. put_byte(dyn_buf, enc);
  98. if (enc == ID3v2_ENCODING_UTF16BOM) {
  99. put_le16(dyn_buf, 0xFEFF); /* BOM */
  100. put = avio_put_str16le;
  101. } else
  102. put = avio_put_str;
  103. put(dyn_buf, str1);
  104. if (str2)
  105. put(dyn_buf, str2);
  106. len = url_close_dyn_buf(dyn_buf, &pb);
  107. put_be32(s->pb, tag);
  108. id3v2_put_size(s, len);
  109. put_be16(s->pb, 0);
  110. put_buffer(s->pb, pb, len);
  111. av_freep(&pb);
  112. return len + ID3v2_HEADER_SIZE;
  113. }
  114. static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  115. {
  116. put_buffer(s->pb, pkt->data, pkt->size);
  117. put_flush_packet(s->pb);
  118. return 0;
  119. }
  120. static int mp3_write_trailer(struct AVFormatContext *s)
  121. {
  122. uint8_t buf[ID3v1_TAG_SIZE];
  123. /* write the id3v1 tag */
  124. if (id3v1_create_tag(s, buf) > 0) {
  125. put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
  126. put_flush_packet(s->pb);
  127. }
  128. return 0;
  129. }
  130. #if CONFIG_MP2_MUXER
  131. AVOutputFormat ff_mp2_muxer = {
  132. "mp2",
  133. NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
  134. "audio/x-mpeg",
  135. "mp2,m2a",
  136. 0,
  137. CODEC_ID_MP2,
  138. CODEC_ID_NONE,
  139. NULL,
  140. mp3_write_packet,
  141. mp3_write_trailer,
  142. };
  143. #endif
  144. #if CONFIG_MP3_MUXER
  145. typedef struct MP3Context {
  146. const AVClass *class;
  147. int id3v2_version;
  148. } MP3Context;
  149. static const AVOption options[] = {
  150. { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
  151. offsetof(MP3Context, id3v2_version), FF_OPT_TYPE_INT, 4, 3, 4, AV_OPT_FLAG_ENCODING_PARAM},
  152. { NULL },
  153. };
  154. static const AVClass mp3_muxer_class = {
  155. "MP3 muxer",
  156. av_default_item_name,
  157. options,
  158. LIBAVUTIL_VERSION_INT,
  159. };
  160. static int id3v2_check_write_tag(AVFormatContext *s, AVMetadataTag *t, const char table[][4],
  161. enum ID3v2Encoding enc)
  162. {
  163. uint32_t tag;
  164. int i;
  165. if (t->key[0] != 'T' || strlen(t->key) != 4)
  166. return -1;
  167. tag = AV_RB32(t->key);
  168. for (i = 0; *table[i]; i++)
  169. if (tag == AV_RB32(table[i]))
  170. return id3v2_put_ttag(s, t->value, NULL, tag, enc);
  171. return -1;
  172. }
  173. /**
  174. * Write an ID3v2 header at beginning of stream
  175. */
  176. static int mp3_write_header(struct AVFormatContext *s)
  177. {
  178. MP3Context *mp3 = s->priv_data;
  179. AVMetadataTag *t = NULL;
  180. int totlen = 0, enc = mp3->id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
  181. ID3v2_ENCODING_UTF8;
  182. int64_t size_pos, cur_pos;
  183. put_be32(s->pb, MKBETAG('I', 'D', '3', mp3->id3v2_version));
  184. put_byte(s->pb, 0);
  185. put_byte(s->pb, 0); /* flags */
  186. /* reserve space for size */
  187. size_pos = url_ftell(s->pb);
  188. put_be32(s->pb, 0);
  189. ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
  190. if (mp3->id3v2_version == 4)
  191. ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
  192. while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
  193. int ret;
  194. if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
  195. totlen += ret;
  196. continue;
  197. }
  198. if ((ret = id3v2_check_write_tag(s, t, mp3->id3v2_version == 3 ?
  199. ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
  200. totlen += ret;
  201. continue;
  202. }
  203. /* unknown tag, write as TXXX frame */
  204. if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
  205. return ret;
  206. totlen += ret;
  207. }
  208. cur_pos = url_ftell(s->pb);
  209. url_fseek(s->pb, size_pos, SEEK_SET);
  210. id3v2_put_size(s, totlen);
  211. url_fseek(s->pb, cur_pos, SEEK_SET);
  212. return 0;
  213. }
  214. AVOutputFormat ff_mp3_muxer = {
  215. "mp3",
  216. NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
  217. "audio/x-mpeg",
  218. "mp3",
  219. sizeof(MP3Context),
  220. CODEC_ID_MP3,
  221. CODEC_ID_NONE,
  222. mp3_write_header,
  223. mp3_write_packet,
  224. mp3_write_trailer,
  225. AVFMT_NOTIMESTAMPS,
  226. .priv_class = &mp3_muxer_class,
  227. };
  228. #endif