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.

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