mp3enc.c 7.1 KB

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