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.

209 lines
5.7KB

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