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.

195 lines
5.4KB

  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. static void id3v2_put_ttag(AVFormatContext *s, const char *buf, int len,
  73. uint32_t tag)
  74. {
  75. put_be32(s->pb, tag);
  76. id3v2_put_size(s, len + 1);
  77. put_be16(s->pb, 0);
  78. put_byte(s->pb, 3); /* UTF-8 */
  79. put_buffer(s->pb, buf, len);
  80. }
  81. static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  82. {
  83. put_buffer(s->pb, pkt->data, pkt->size);
  84. put_flush_packet(s->pb);
  85. return 0;
  86. }
  87. static int mp3_write_trailer(struct AVFormatContext *s)
  88. {
  89. uint8_t buf[ID3v1_TAG_SIZE];
  90. /* write the id3v1 tag */
  91. if (id3v1_create_tag(s, buf) > 0) {
  92. put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
  93. put_flush_packet(s->pb);
  94. }
  95. return 0;
  96. }
  97. #if CONFIG_MP2_MUXER
  98. AVOutputFormat mp2_muxer = {
  99. "mp2",
  100. NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
  101. "audio/x-mpeg",
  102. "mp2,m2a",
  103. 0,
  104. CODEC_ID_MP2,
  105. CODEC_ID_NONE,
  106. NULL,
  107. mp3_write_packet,
  108. mp3_write_trailer,
  109. };
  110. #endif
  111. #if CONFIG_MP3_MUXER
  112. /**
  113. * Write an ID3v2.4 header at beginning of stream
  114. */
  115. static int mp3_write_header(struct AVFormatContext *s)
  116. {
  117. AVMetadataTag *t = NULL;
  118. int totlen = 0;
  119. int64_t size_pos, cur_pos;
  120. put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
  121. put_byte(s->pb, 0);
  122. put_byte(s->pb, 0); /* flags */
  123. /* reserve space for size */
  124. size_pos = url_ftell(s->pb);
  125. put_be32(s->pb, 0);
  126. ff_metadata_conv(&s->metadata, ff_id3v2_metadata_conv, NULL);
  127. while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX))) {
  128. uint32_t tag = 0;
  129. if (t->key[0] == 'T' && strlen(t->key) == 4) {
  130. int i;
  131. for (i = 0; *ff_id3v2_tags[i]; i++)
  132. if (AV_RB32(t->key) == AV_RB32(ff_id3v2_tags[i])) {
  133. int len = strlen(t->value);
  134. tag = AV_RB32(t->key);
  135. totlen += len + ID3v2_HEADER_SIZE + 2;
  136. id3v2_put_ttag(s, t->value, len + 1, tag);
  137. break;
  138. }
  139. }
  140. if (!tag) { /* unknown tag, write as TXXX frame */
  141. int len = strlen(t->key), len1 = strlen(t->value);
  142. char *buf = av_malloc(len + len1 + 2);
  143. if (!buf)
  144. return AVERROR(ENOMEM);
  145. tag = MKBETAG('T', 'X', 'X', 'X');
  146. strcpy(buf, t->key);
  147. strcpy(buf + len + 1, t->value);
  148. id3v2_put_ttag(s, buf, len + len1 + 2, tag);
  149. totlen += len + len1 + ID3v2_HEADER_SIZE + 3;
  150. av_free(buf);
  151. }
  152. }
  153. cur_pos = url_ftell(s->pb);
  154. url_fseek(s->pb, size_pos, SEEK_SET);
  155. id3v2_put_size(s, totlen);
  156. url_fseek(s->pb, cur_pos, SEEK_SET);
  157. return 0;
  158. }
  159. AVOutputFormat mp3_muxer = {
  160. "mp3",
  161. NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
  162. "audio/x-mpeg",
  163. "mp3",
  164. 0,
  165. CODEC_ID_MP3,
  166. CODEC_ID_NONE,
  167. mp3_write_header,
  168. mp3_write_packet,
  169. mp3_write_trailer,
  170. AVFMT_NOTIMESTAMPS,
  171. };
  172. #endif