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.

630 lines
16KB

  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 "avformat.h"
  22. #include "mpegaudio.h"
  23. #include "avstring.h"
  24. #define ID3v2_HEADER_SIZE 10
  25. #define ID3v1_TAG_SIZE 128
  26. #define ID3v1_GENRE_MAX 125
  27. static const char *id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
  28. [0] = "Blues",
  29. [1] = "Classic Rock",
  30. [2] = "Country",
  31. [3] = "Dance",
  32. [4] = "Disco",
  33. [5] = "Funk",
  34. [6] = "Grunge",
  35. [7] = "Hip-Hop",
  36. [8] = "Jazz",
  37. [9] = "Metal",
  38. [10] = "New Age",
  39. [11] = "Oldies",
  40. [12] = "Other",
  41. [13] = "Pop",
  42. [14] = "R&B",
  43. [15] = "Rap",
  44. [16] = "Reggae",
  45. [17] = "Rock",
  46. [18] = "Techno",
  47. [19] = "Industrial",
  48. [20] = "Alternative",
  49. [21] = "Ska",
  50. [22] = "Death Metal",
  51. [23] = "Pranks",
  52. [24] = "Soundtrack",
  53. [25] = "Euro-Techno",
  54. [26] = "Ambient",
  55. [27] = "Trip-Hop",
  56. [28] = "Vocal",
  57. [29] = "Jazz+Funk",
  58. [30] = "Fusion",
  59. [31] = "Trance",
  60. [32] = "Classical",
  61. [33] = "Instrumental",
  62. [34] = "Acid",
  63. [35] = "House",
  64. [36] = "Game",
  65. [37] = "Sound Clip",
  66. [38] = "Gospel",
  67. [39] = "Noise",
  68. [40] = "AlternRock",
  69. [41] = "Bass",
  70. [42] = "Soul",
  71. [43] = "Punk",
  72. [44] = "Space",
  73. [45] = "Meditative",
  74. [46] = "Instrumental Pop",
  75. [47] = "Instrumental Rock",
  76. [48] = "Ethnic",
  77. [49] = "Gothic",
  78. [50] = "Darkwave",
  79. [51] = "Techno-Industrial",
  80. [52] = "Electronic",
  81. [53] = "Pop-Folk",
  82. [54] = "Eurodance",
  83. [55] = "Dream",
  84. [56] = "Southern Rock",
  85. [57] = "Comedy",
  86. [58] = "Cult",
  87. [59] = "Gangsta",
  88. [60] = "Top 40",
  89. [61] = "Christian Rap",
  90. [62] = "Pop/Funk",
  91. [63] = "Jungle",
  92. [64] = "Native American",
  93. [65] = "Cabaret",
  94. [66] = "New Wave",
  95. [67] = "Psychadelic",
  96. [68] = "Rave",
  97. [69] = "Showtunes",
  98. [70] = "Trailer",
  99. [71] = "Lo-Fi",
  100. [72] = "Tribal",
  101. [73] = "Acid Punk",
  102. [74] = "Acid Jazz",
  103. [75] = "Polka",
  104. [76] = "Retro",
  105. [77] = "Musical",
  106. [78] = "Rock & Roll",
  107. [79] = "Hard Rock",
  108. [80] = "Folk",
  109. [81] = "Folk-Rock",
  110. [82] = "National Folk",
  111. [83] = "Swing",
  112. [84] = "Fast Fusion",
  113. [85] = "Bebob",
  114. [86] = "Latin",
  115. [87] = "Revival",
  116. [88] = "Celtic",
  117. [89] = "Bluegrass",
  118. [90] = "Avantgarde",
  119. [91] = "Gothic Rock",
  120. [92] = "Progressive Rock",
  121. [93] = "Psychedelic Rock",
  122. [94] = "Symphonic Rock",
  123. [95] = "Slow Rock",
  124. [96] = "Big Band",
  125. [97] = "Chorus",
  126. [98] = "Easy Listening",
  127. [99] = "Acoustic",
  128. [100] = "Humour",
  129. [101] = "Speech",
  130. [102] = "Chanson",
  131. [103] = "Opera",
  132. [104] = "Chamber Music",
  133. [105] = "Sonata",
  134. [106] = "Symphony",
  135. [107] = "Booty Bass",
  136. [108] = "Primus",
  137. [109] = "Porn Groove",
  138. [110] = "Satire",
  139. [111] = "Slow Jam",
  140. [112] = "Club",
  141. [113] = "Tango",
  142. [114] = "Samba",
  143. [115] = "Folklore",
  144. [116] = "Ballad",
  145. [117] = "Power Ballad",
  146. [118] = "Rhythmic Soul",
  147. [119] = "Freestyle",
  148. [120] = "Duet",
  149. [121] = "Punk Rock",
  150. [122] = "Drum Solo",
  151. [123] = "A capella",
  152. [124] = "Euro-House",
  153. [125] = "Dance Hall",
  154. };
  155. /* buf must be ID3v2_HEADER_SIZE byte long */
  156. static int id3v2_match(const uint8_t *buf)
  157. {
  158. return (buf[0] == 'I' &&
  159. buf[1] == 'D' &&
  160. buf[2] == '3' &&
  161. buf[3] != 0xff &&
  162. buf[4] != 0xff &&
  163. (buf[6] & 0x80) == 0 &&
  164. (buf[7] & 0x80) == 0 &&
  165. (buf[8] & 0x80) == 0 &&
  166. (buf[9] & 0x80) == 0);
  167. }
  168. static unsigned int id3v2_get_size(ByteIOContext *s, int len)
  169. {
  170. int v=0;
  171. while(len--)
  172. v= (v<<7) + (get_byte(s)&0x7F);
  173. return v;
  174. }
  175. static void id3v2_read_ttag(AVFormatContext *s, int taglen, char *dst, int dstlen)
  176. {
  177. char *q;
  178. int len;
  179. if(taglen < 1)
  180. return;
  181. taglen--; /* account for encoding type byte */
  182. dstlen--; /* Leave space for zero terminator */
  183. switch(get_byte(&s->pb)) { /* encoding type */
  184. case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
  185. q = dst;
  186. while(taglen--) {
  187. uint8_t tmp;
  188. PUT_UTF8(get_byte(&s->pb), tmp, if (q - dst < dstlen - 1) *q++ = tmp;)
  189. }
  190. *q = '\0';
  191. break;
  192. case 3: /* UTF-8 */
  193. len = FFMIN(taglen, dstlen);
  194. get_buffer(&s->pb, dst, len);
  195. dst[len] = 0;
  196. break;
  197. }
  198. }
  199. /**
  200. * ID3v2 parser
  201. *
  202. * Handles ID3v2.2, 2.3 and 2.4.
  203. *
  204. */
  205. static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
  206. {
  207. int isv34, tlen;
  208. uint32_t tag;
  209. offset_t next;
  210. char tmp[16];
  211. int taghdrlen;
  212. const char *reason;
  213. switch(version) {
  214. case 2:
  215. if(flags & 0x40) {
  216. reason = "compression";
  217. goto error;
  218. }
  219. isv34 = 0;
  220. taghdrlen = 6;
  221. break;
  222. case 3:
  223. case 4:
  224. isv34 = 1;
  225. taghdrlen = 10;
  226. break;
  227. default:
  228. reason = "version";
  229. goto error;
  230. }
  231. if(flags & 0x80) {
  232. reason = "unsynchronization";
  233. goto error;
  234. }
  235. if(isv34 && flags & 0x40) /* Extended header present, just skip over it */
  236. url_fskip(&s->pb, id3v2_get_size(&s->pb, 4));
  237. while(len >= taghdrlen) {
  238. if(isv34) {
  239. tag = get_be32(&s->pb);
  240. tlen = id3v2_get_size(&s->pb, 4);
  241. get_be16(&s->pb); /* flags */
  242. } else {
  243. tag = get_be24(&s->pb);
  244. tlen = id3v2_get_size(&s->pb, 3);
  245. }
  246. len -= taghdrlen + tlen;
  247. if(len < 0)
  248. break;
  249. next = url_ftell(&s->pb) + tlen;
  250. switch(tag) {
  251. case MKBETAG('T', 'I', 'T', '2'):
  252. case MKBETAG(0, 'T', 'T', '2'):
  253. id3v2_read_ttag(s, tlen, s->title, sizeof(s->title));
  254. break;
  255. case MKBETAG('T', 'P', 'E', '1'):
  256. case MKBETAG(0, 'T', 'P', '1'):
  257. id3v2_read_ttag(s, tlen, s->author, sizeof(s->author));
  258. break;
  259. case MKBETAG('T', 'A', 'L', 'B'):
  260. case MKBETAG(0, 'T', 'A', 'L'):
  261. id3v2_read_ttag(s, tlen, s->album, sizeof(s->album));
  262. break;
  263. case MKBETAG('T', 'C', 'O', 'N'):
  264. case MKBETAG(0, 'T', 'C', 'O'):
  265. id3v2_read_ttag(s, tlen, s->genre, sizeof(s->genre));
  266. break;
  267. case MKBETAG('T', 'C', 'O', 'P'):
  268. case MKBETAG(0, 'T', 'C', 'R'):
  269. id3v2_read_ttag(s, tlen, s->copyright, sizeof(s->copyright));
  270. break;
  271. case MKBETAG('T', 'R', 'C', 'K'):
  272. case MKBETAG(0, 'T', 'R', 'K'):
  273. id3v2_read_ttag(s, tlen, tmp, sizeof(tmp));
  274. s->track = atoi(tmp);
  275. break;
  276. case 0:
  277. /* padding, skip to end */
  278. url_fskip(&s->pb, len);
  279. len = 0;
  280. continue;
  281. }
  282. /* Skip to end of tag */
  283. url_fseek(&s->pb, next, SEEK_SET);
  284. }
  285. if(version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  286. url_fskip(&s->pb, 10);
  287. return;
  288. error:
  289. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  290. url_fskip(&s->pb, len);
  291. }
  292. static void id3v1_get_string(char *str, int str_size,
  293. const uint8_t *buf, int buf_size)
  294. {
  295. int i, c;
  296. char *q;
  297. q = str;
  298. for(i = 0; i < buf_size; i++) {
  299. c = buf[i];
  300. if (c == '\0')
  301. break;
  302. if ((q - str) >= str_size - 1)
  303. break;
  304. *q++ = c;
  305. }
  306. *q = '\0';
  307. }
  308. /* 'buf' must be ID3v1_TAG_SIZE byte long */
  309. static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf)
  310. {
  311. char str[5];
  312. int genre;
  313. if (!(buf[0] == 'T' &&
  314. buf[1] == 'A' &&
  315. buf[2] == 'G'))
  316. return -1;
  317. id3v1_get_string(s->title, sizeof(s->title), buf + 3, 30);
  318. id3v1_get_string(s->author, sizeof(s->author), buf + 33, 30);
  319. id3v1_get_string(s->album, sizeof(s->album), buf + 63, 30);
  320. id3v1_get_string(str, sizeof(str), buf + 93, 4);
  321. s->year = atoi(str);
  322. id3v1_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
  323. if (buf[125] == 0 && buf[126] != 0)
  324. s->track = buf[126];
  325. genre = buf[127];
  326. if (genre <= ID3v1_GENRE_MAX)
  327. av_strlcpy(s->genre, id3v1_genre_str[genre], sizeof(s->genre));
  328. return 0;
  329. }
  330. static void id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
  331. {
  332. int v, i;
  333. memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
  334. buf[0] = 'T';
  335. buf[1] = 'A';
  336. buf[2] = 'G';
  337. strncpy(buf + 3, s->title, 30);
  338. strncpy(buf + 33, s->author, 30);
  339. strncpy(buf + 63, s->album, 30);
  340. v = s->year;
  341. if (v > 0) {
  342. for(i = 0;i < 4; i++) {
  343. buf[96 - i] = '0' + (v % 10);
  344. v = v / 10;
  345. }
  346. }
  347. strncpy(buf + 97, s->comment, 30);
  348. if (s->track != 0) {
  349. buf[125] = 0;
  350. buf[126] = s->track;
  351. }
  352. for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
  353. if (!strcasecmp(s->genre, id3v1_genre_str[i])) {
  354. buf[127] = i;
  355. break;
  356. }
  357. }
  358. }
  359. /* mp3 read */
  360. static int mp3_read_probe(AVProbeData *p)
  361. {
  362. int max_frames, first_frames = 0;
  363. int fsize, frames, sample_rate;
  364. uint32_t header;
  365. uint8_t *buf, *buf2, *end;
  366. AVCodecContext avctx;
  367. if(id3v2_match(p->buf))
  368. return AVPROBE_SCORE_MAX/2+1; // this must be less than mpeg-ps because some retards put id3v2 tags before mpeg-ps files
  369. max_frames = 0;
  370. buf = p->buf;
  371. end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
  372. for(; buf < end; buf++) {
  373. buf2 = buf;
  374. for(frames = 0; buf2 < end; frames++) {
  375. header = AV_RB32(buf2);
  376. fsize = ff_mpa_decode_header(&avctx, header, &sample_rate);
  377. if(fsize < 0)
  378. break;
  379. buf2 += fsize;
  380. }
  381. max_frames = FFMAX(max_frames, frames);
  382. if(buf == p->buf)
  383. first_frames= frames;
  384. }
  385. if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
  386. else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
  387. else if(max_frames>=1) return 1;
  388. else return 0;
  389. }
  390. static int mp3_read_header(AVFormatContext *s,
  391. AVFormatParameters *ap)
  392. {
  393. AVStream *st;
  394. uint8_t buf[ID3v1_TAG_SIZE];
  395. int len, ret, filesize;
  396. st = av_new_stream(s, 0);
  397. if (!st)
  398. return AVERROR(ENOMEM);
  399. st->codec->codec_type = CODEC_TYPE_AUDIO;
  400. st->codec->codec_id = CODEC_ID_MP3;
  401. st->need_parsing = AVSTREAM_PARSE_FULL;
  402. /* try to get the TAG */
  403. if (!url_is_streamed(&s->pb)) {
  404. /* XXX: change that */
  405. filesize = url_fsize(&s->pb);
  406. if (filesize > 128) {
  407. url_fseek(&s->pb, filesize - 128, SEEK_SET);
  408. ret = get_buffer(&s->pb, buf, ID3v1_TAG_SIZE);
  409. if (ret == ID3v1_TAG_SIZE) {
  410. id3v1_parse_tag(s, buf);
  411. }
  412. url_fseek(&s->pb, 0, SEEK_SET);
  413. }
  414. }
  415. /* if ID3v2 header found, skip it */
  416. ret = get_buffer(&s->pb, buf, ID3v2_HEADER_SIZE);
  417. if (ret != ID3v2_HEADER_SIZE)
  418. return -1;
  419. if (id3v2_match(buf)) {
  420. /* parse ID3v2 header */
  421. len = ((buf[6] & 0x7f) << 21) |
  422. ((buf[7] & 0x7f) << 14) |
  423. ((buf[8] & 0x7f) << 7) |
  424. (buf[9] & 0x7f);
  425. id3v2_parse(s, len, buf[3], buf[5]);
  426. } else {
  427. url_fseek(&s->pb, 0, SEEK_SET);
  428. }
  429. /* the parameters will be extracted from the compressed bitstream */
  430. return 0;
  431. }
  432. #define MP3_PACKET_SIZE 1024
  433. static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
  434. {
  435. int ret, size;
  436. // AVStream *st = s->streams[0];
  437. size= MP3_PACKET_SIZE;
  438. ret= av_get_packet(&s->pb, pkt, size);
  439. pkt->stream_index = 0;
  440. if (ret <= 0) {
  441. return AVERROR(EIO);
  442. }
  443. /* note: we need to modify the packet size here to handle the last
  444. packet */
  445. pkt->size = ret;
  446. return ret;
  447. }
  448. static int mp3_read_close(AVFormatContext *s)
  449. {
  450. return 0;
  451. }
  452. #ifdef CONFIG_MUXERS
  453. /* simple formats */
  454. static void id3v2_put_size(AVFormatContext *s, int size)
  455. {
  456. put_byte(&s->pb, size >> 21 & 0x7f);
  457. put_byte(&s->pb, size >> 14 & 0x7f);
  458. put_byte(&s->pb, size >> 7 & 0x7f);
  459. put_byte(&s->pb, size & 0x7f);
  460. }
  461. static void id3v2_put_ttag(AVFormatContext *s, char *string, uint32_t tag)
  462. {
  463. int len = strlen(string);
  464. put_be32(&s->pb, tag);
  465. id3v2_put_size(s, len + 1);
  466. put_be16(&s->pb, 0);
  467. put_byte(&s->pb, 3); /* UTF-8 */
  468. put_buffer(&s->pb, string, len);
  469. }
  470. /**
  471. * Write an ID3v2.4 header at beginning of stream
  472. */
  473. static int mp3_write_header(struct AVFormatContext *s)
  474. {
  475. int totlen = 0;
  476. char tracktxt[10];
  477. if(s->track)
  478. snprintf(tracktxt, sizeof(tracktxt) - 1, "%d", s->track);
  479. if(s->title[0]) totlen += 11 + strlen(s->title);
  480. if(s->author[0]) totlen += 11 + strlen(s->author);
  481. if(s->album[0]) totlen += 11 + strlen(s->album);
  482. if(s->genre[0]) totlen += 11 + strlen(s->genre);
  483. if(s->copyright[0]) totlen += 11 + strlen(s->copyright);
  484. if(s->track) totlen += 11 + strlen(tracktxt);
  485. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  486. totlen += strlen(LIBAVFORMAT_IDENT) + 11;
  487. if(totlen == 0)
  488. return 0;
  489. put_be32(&s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
  490. put_byte(&s->pb, 0);
  491. put_byte(&s->pb, 0); /* flags */
  492. id3v2_put_size(s, totlen);
  493. if(s->title[0]) id3v2_put_ttag(s, s->title, MKBETAG('T', 'I', 'T', '2'));
  494. if(s->author[0]) id3v2_put_ttag(s, s->author, MKBETAG('T', 'P', 'E', '1'));
  495. if(s->album[0]) id3v2_put_ttag(s, s->album, MKBETAG('T', 'A', 'L', 'B'));
  496. if(s->genre[0]) id3v2_put_ttag(s, s->genre, MKBETAG('T', 'C', 'O', 'N'));
  497. if(s->copyright[0]) id3v2_put_ttag(s, s->copyright, MKBETAG('T', 'C', 'O', 'P'));
  498. if(s->track) id3v2_put_ttag(s, tracktxt, MKBETAG('T', 'R', 'C', 'K'));
  499. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  500. id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C'));
  501. return 0;
  502. }
  503. static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  504. {
  505. put_buffer(&s->pb, pkt->data, pkt->size);
  506. put_flush_packet(&s->pb);
  507. return 0;
  508. }
  509. static int mp3_write_trailer(struct AVFormatContext *s)
  510. {
  511. uint8_t buf[ID3v1_TAG_SIZE];
  512. /* write the id3v1 tag */
  513. if (s->title[0] != '\0') {
  514. id3v1_create_tag(s, buf);
  515. put_buffer(&s->pb, buf, ID3v1_TAG_SIZE);
  516. put_flush_packet(&s->pb);
  517. }
  518. return 0;
  519. }
  520. #endif //CONFIG_MUXERS
  521. #ifdef CONFIG_MP3_DEMUXER
  522. AVInputFormat mp3_demuxer = {
  523. "mp3",
  524. "MPEG audio",
  525. 0,
  526. mp3_read_probe,
  527. mp3_read_header,
  528. mp3_read_packet,
  529. mp3_read_close,
  530. .flags= AVFMT_GENERIC_INDEX,
  531. .extensions = "mp2,mp3,m2a", /* XXX: use probe */
  532. };
  533. #endif
  534. #ifdef CONFIG_MP2_MUXER
  535. AVOutputFormat mp2_muxer = {
  536. "mp2",
  537. "MPEG audio layer 2",
  538. "audio/x-mpeg",
  539. #ifdef CONFIG_LIBMP3LAME
  540. "mp2,m2a",
  541. #else
  542. "mp2,mp3,m2a",
  543. #endif
  544. 0,
  545. CODEC_ID_MP2,
  546. 0,
  547. NULL,
  548. mp3_write_packet,
  549. mp3_write_trailer,
  550. };
  551. #endif
  552. #ifdef CONFIG_MP3_MUXER
  553. AVOutputFormat mp3_muxer = {
  554. "mp3",
  555. "MPEG audio layer 3",
  556. "audio/x-mpeg",
  557. "mp3",
  558. 0,
  559. CODEC_ID_MP3,
  560. 0,
  561. mp3_write_header,
  562. mp3_write_packet,
  563. mp3_write_trailer,
  564. };
  565. #endif