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.

416 lines
12KB

  1. /*
  2. * MP3 muxer and demuxer
  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 "libavutil/avstring.h"
  23. #include "libavcodec/mpegaudio.h"
  24. #include "libavcodec/mpegaudiodecheader.h"
  25. #include "avformat.h"
  26. #include "id3v2.h"
  27. #include "id3v1.h"
  28. static void id3v1_get_string(AVFormatContext *s, const char *key,
  29. const uint8_t *buf, int buf_size)
  30. {
  31. int i, c;
  32. char *q, str[512];
  33. q = str;
  34. for(i = 0; i < buf_size; i++) {
  35. c = buf[i];
  36. if (c == '\0')
  37. break;
  38. if ((q - str) >= sizeof(str) - 1)
  39. break;
  40. *q++ = c;
  41. }
  42. *q = '\0';
  43. if (*str)
  44. av_metadata_set(&s->metadata, key, str);
  45. }
  46. /* 'buf' must be ID3v1_TAG_SIZE byte long */
  47. static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf)
  48. {
  49. char str[5];
  50. int genre;
  51. if (!(buf[0] == 'T' &&
  52. buf[1] == 'A' &&
  53. buf[2] == 'G'))
  54. return -1;
  55. id3v1_get_string(s, "title", buf + 3, 30);
  56. id3v1_get_string(s, "author", buf + 33, 30);
  57. id3v1_get_string(s, "album", buf + 63, 30);
  58. id3v1_get_string(s, "year", buf + 93, 4);
  59. id3v1_get_string(s, "comment", buf + 97, 30);
  60. if (buf[125] == 0 && buf[126] != 0) {
  61. snprintf(str, sizeof(str), "%d", buf[126]);
  62. av_metadata_set(&s->metadata, "track", str);
  63. }
  64. genre = buf[127];
  65. if (genre <= ID3v1_GENRE_MAX)
  66. av_metadata_set(&s->metadata, "genre", ff_id3v1_genre_str[genre]);
  67. return 0;
  68. }
  69. /* mp3 read */
  70. static int mp3_read_probe(AVProbeData *p)
  71. {
  72. int max_frames, first_frames = 0;
  73. int fsize, frames, sample_rate;
  74. uint32_t header;
  75. uint8_t *buf, *buf0, *buf2, *end;
  76. AVCodecContext avctx;
  77. buf0 = p->buf;
  78. if(ff_id3v2_match(buf0)) {
  79. buf0 += ff_id3v2_tag_len(buf0);
  80. }
  81. max_frames = 0;
  82. buf = buf0;
  83. end = p->buf + p->buf_size - sizeof(uint32_t);
  84. for(; buf < end; buf= buf2+1) {
  85. buf2 = buf;
  86. for(frames = 0; buf2 < end; frames++) {
  87. header = AV_RB32(buf2);
  88. fsize = ff_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
  89. if(fsize < 0)
  90. break;
  91. buf2 += fsize;
  92. }
  93. max_frames = FFMAX(max_frames, frames);
  94. if(buf == buf0)
  95. first_frames= frames;
  96. }
  97. if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
  98. else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
  99. else if(max_frames>=4) return AVPROBE_SCORE_MAX/4;
  100. else if(buf0!=p->buf) return AVPROBE_SCORE_MAX/4-1;
  101. else if(max_frames>=1) return 1;
  102. else return 0;
  103. //mpegps_mp3_unrecognized_format.mpg has max_frames=3
  104. }
  105. /**
  106. * Try to find Xing/Info/VBRI tags and compute duration from info therein
  107. */
  108. static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
  109. {
  110. uint32_t v, spf;
  111. int frames = -1; /* Total number of frames in file */
  112. const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
  113. MPADecodeHeader c;
  114. int vbrtag_size = 0;
  115. v = get_be32(s->pb);
  116. if(ff_mpa_check_header(v) < 0)
  117. return -1;
  118. if (ff_mpegaudio_decode_header(&c, v) == 0)
  119. vbrtag_size = c.frame_size;
  120. if(c.layer != 3)
  121. return -1;
  122. /* Check for Xing / Info tag */
  123. url_fseek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
  124. v = get_be32(s->pb);
  125. if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
  126. v = get_be32(s->pb);
  127. if(v & 0x1)
  128. frames = get_be32(s->pb);
  129. }
  130. /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
  131. url_fseek(s->pb, base + 4 + 32, SEEK_SET);
  132. v = get_be32(s->pb);
  133. if(v == MKBETAG('V', 'B', 'R', 'I')) {
  134. /* Check tag version */
  135. if(get_be16(s->pb) == 1) {
  136. /* skip delay, quality and total bytes */
  137. url_fseek(s->pb, 8, SEEK_CUR);
  138. frames = get_be32(s->pb);
  139. }
  140. }
  141. if(frames < 0)
  142. return -1;
  143. /* Skip the vbr tag frame */
  144. url_fseek(s->pb, base + vbrtag_size, SEEK_SET);
  145. spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
  146. st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
  147. st->time_base);
  148. return 0;
  149. }
  150. static int mp3_read_header(AVFormatContext *s,
  151. AVFormatParameters *ap)
  152. {
  153. AVStream *st;
  154. uint8_t buf[ID3v1_TAG_SIZE];
  155. int len, ret, filesize;
  156. int64_t off;
  157. st = av_new_stream(s, 0);
  158. if (!st)
  159. return AVERROR(ENOMEM);
  160. st->codec->codec_type = CODEC_TYPE_AUDIO;
  161. st->codec->codec_id = CODEC_ID_MP3;
  162. st->need_parsing = AVSTREAM_PARSE_FULL;
  163. st->start_time = 0;
  164. /* try to get the TAG */
  165. if (!url_is_streamed(s->pb)) {
  166. /* XXX: change that */
  167. filesize = url_fsize(s->pb);
  168. if (filesize > 128) {
  169. url_fseek(s->pb, filesize - 128, SEEK_SET);
  170. ret = get_buffer(s->pb, buf, ID3v1_TAG_SIZE);
  171. if (ret == ID3v1_TAG_SIZE) {
  172. id3v1_parse_tag(s, buf);
  173. }
  174. url_fseek(s->pb, 0, SEEK_SET);
  175. }
  176. }
  177. /* if ID3v2 header found, skip it */
  178. ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
  179. if (ret != ID3v2_HEADER_SIZE)
  180. return -1;
  181. if (ff_id3v2_match(buf)) {
  182. /* parse ID3v2 header */
  183. len = ((buf[6] & 0x7f) << 21) |
  184. ((buf[7] & 0x7f) << 14) |
  185. ((buf[8] & 0x7f) << 7) |
  186. (buf[9] & 0x7f);
  187. ff_id3v2_parse(s, len, buf[3], buf[5]);
  188. } else {
  189. url_fseek(s->pb, 0, SEEK_SET);
  190. }
  191. off = url_ftell(s->pb);
  192. if (mp3_parse_vbr_tags(s, st, off) < 0)
  193. url_fseek(s->pb, off, SEEK_SET);
  194. /* the parameters will be extracted from the compressed bitstream */
  195. return 0;
  196. }
  197. #define MP3_PACKET_SIZE 1024
  198. static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
  199. {
  200. int ret, size;
  201. // AVStream *st = s->streams[0];
  202. size= MP3_PACKET_SIZE;
  203. ret= av_get_packet(s->pb, pkt, size);
  204. pkt->stream_index = 0;
  205. if (ret <= 0) {
  206. return AVERROR(EIO);
  207. }
  208. /* note: we need to modify the packet size here to handle the last
  209. packet */
  210. pkt->size = ret;
  211. return ret;
  212. }
  213. #if CONFIG_MP2_MUXER || CONFIG_MP3_MUXER
  214. static int id3v1_set_string(AVFormatContext *s, const char *key,
  215. uint8_t *buf, int buf_size)
  216. {
  217. AVMetadataTag *tag;
  218. if ((tag = av_metadata_get(s->metadata, key, NULL, 0)))
  219. strncpy(buf, tag->value, buf_size);
  220. return !!tag;
  221. }
  222. static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
  223. {
  224. AVMetadataTag *tag;
  225. int i, count = 0;
  226. memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
  227. buf[0] = 'T';
  228. buf[1] = 'A';
  229. buf[2] = 'G';
  230. count += id3v1_set_string(s, "title", buf + 3, 30);
  231. count += id3v1_set_string(s, "author", buf + 33, 30);
  232. count += id3v1_set_string(s, "album", buf + 63, 30);
  233. count += id3v1_set_string(s, "year", buf + 93, 4);
  234. count += id3v1_set_string(s, "comment", buf + 97, 30);
  235. if ((tag = av_metadata_get(s->metadata, "track", NULL, 0))) {
  236. buf[125] = 0;
  237. buf[126] = atoi(tag->value);
  238. count++;
  239. }
  240. if ((tag = av_metadata_get(s->metadata, "genre", NULL, 0))) {
  241. for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
  242. if (!strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
  243. buf[127] = i;
  244. count++;
  245. break;
  246. }
  247. }
  248. }
  249. return count;
  250. }
  251. /* simple formats */
  252. static void id3v2_put_size(AVFormatContext *s, int size)
  253. {
  254. put_byte(s->pb, size >> 21 & 0x7f);
  255. put_byte(s->pb, size >> 14 & 0x7f);
  256. put_byte(s->pb, size >> 7 & 0x7f);
  257. put_byte(s->pb, size & 0x7f);
  258. }
  259. static void id3v2_put_ttag(AVFormatContext *s, const char *string, uint32_t tag)
  260. {
  261. int len = strlen(string);
  262. put_be32(s->pb, tag);
  263. id3v2_put_size(s, len + 1);
  264. put_be16(s->pb, 0);
  265. put_byte(s->pb, 3); /* UTF-8 */
  266. put_buffer(s->pb, string, len);
  267. }
  268. /**
  269. * Write an ID3v2.4 header at beginning of stream
  270. */
  271. static int mp3_write_header(struct AVFormatContext *s)
  272. {
  273. AVMetadataTag *title, *author, *album, *genre, *copyright, *track, *year;
  274. int totlen = 0;
  275. title = av_metadata_get(s->metadata, "title", NULL, 0);
  276. author = av_metadata_get(s->metadata, "author", NULL, 0);
  277. album = av_metadata_get(s->metadata, "album", NULL, 0);
  278. genre = av_metadata_get(s->metadata, "genre", NULL, 0);
  279. copyright = av_metadata_get(s->metadata, "copyright", NULL, 0);
  280. track = av_metadata_get(s->metadata, "track", NULL, 0);
  281. year = av_metadata_get(s->metadata, "year", NULL, 0);
  282. if(title) totlen += 11 + strlen(title->value);
  283. if(author) totlen += 11 + strlen(author->value);
  284. if(album) totlen += 11 + strlen(album->value);
  285. if(genre) totlen += 11 + strlen(genre->value);
  286. if(copyright) totlen += 11 + strlen(copyright->value);
  287. if(track) totlen += 11 + strlen(track->value);
  288. if(year) totlen += 11 + strlen(year->value);
  289. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  290. totlen += strlen(LIBAVFORMAT_IDENT) + 11;
  291. if(totlen == 0)
  292. return 0;
  293. put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
  294. put_byte(s->pb, 0);
  295. put_byte(s->pb, 0); /* flags */
  296. id3v2_put_size(s, totlen);
  297. if(title) id3v2_put_ttag(s, title->value, MKBETAG('T', 'I', 'T', '2'));
  298. if(author) id3v2_put_ttag(s, author->value, MKBETAG('T', 'P', 'E', '1'));
  299. if(album) id3v2_put_ttag(s, album->value, MKBETAG('T', 'A', 'L', 'B'));
  300. if(genre) id3v2_put_ttag(s, genre->value, MKBETAG('T', 'C', 'O', 'N'));
  301. if(copyright) id3v2_put_ttag(s, copyright->value, MKBETAG('T', 'C', 'O', 'P'));
  302. if(track) id3v2_put_ttag(s, track->value, MKBETAG('T', 'R', 'C', 'K'));
  303. if(year) id3v2_put_ttag(s, year->value, MKBETAG('T', 'Y', 'E', 'R'));
  304. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  305. id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C'));
  306. return 0;
  307. }
  308. static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  309. {
  310. put_buffer(s->pb, pkt->data, pkt->size);
  311. put_flush_packet(s->pb);
  312. return 0;
  313. }
  314. static int mp3_write_trailer(struct AVFormatContext *s)
  315. {
  316. uint8_t buf[ID3v1_TAG_SIZE];
  317. /* write the id3v1 tag */
  318. if (id3v1_create_tag(s, buf) > 0) {
  319. put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
  320. put_flush_packet(s->pb);
  321. }
  322. return 0;
  323. }
  324. #endif /* CONFIG_MP2_MUXER || CONFIG_MP3_MUXER */
  325. #if CONFIG_MP3_DEMUXER
  326. AVInputFormat mp3_demuxer = {
  327. "mp3",
  328. NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
  329. 0,
  330. mp3_read_probe,
  331. mp3_read_header,
  332. mp3_read_packet,
  333. .flags= AVFMT_GENERIC_INDEX,
  334. .extensions = "mp2,mp3,m2a", /* XXX: use probe */
  335. };
  336. #endif
  337. #if CONFIG_MP2_MUXER
  338. AVOutputFormat mp2_muxer = {
  339. "mp2",
  340. NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
  341. "audio/x-mpeg",
  342. "mp2,m2a",
  343. 0,
  344. CODEC_ID_MP2,
  345. CODEC_ID_NONE,
  346. NULL,
  347. mp3_write_packet,
  348. mp3_write_trailer,
  349. };
  350. #endif
  351. #if CONFIG_MP3_MUXER
  352. AVOutputFormat mp3_muxer = {
  353. "mp3",
  354. NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
  355. "audio/x-mpeg",
  356. "mp3",
  357. 0,
  358. CODEC_ID_MP3,
  359. CODEC_ID_NONE,
  360. mp3_write_header,
  361. mp3_write_packet,
  362. mp3_write_trailer,
  363. };
  364. #endif