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.

432 lines
10KB

  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 ID3_HEADER_SIZE 10
  24. #define ID3_TAG_SIZE 128
  25. #define ID3_GENRE_MAX 125
  26. static const char *id3_genre_str[ID3_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 ID3_HEADER_SIZE byte long */
  155. static int id3_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 void id3_get_string(char *str, int str_size,
  168. const uint8_t *buf, int buf_size)
  169. {
  170. int i, c;
  171. char *q;
  172. q = str;
  173. for(i = 0; i < buf_size; i++) {
  174. c = buf[i];
  175. if (c == '\0')
  176. break;
  177. if ((q - str) >= str_size - 1)
  178. break;
  179. *q++ = c;
  180. }
  181. *q = '\0';
  182. }
  183. /* 'buf' must be ID3_TAG_SIZE byte long */
  184. static int id3_parse_tag(AVFormatContext *s, const uint8_t *buf)
  185. {
  186. char str[5];
  187. int genre;
  188. if (!(buf[0] == 'T' &&
  189. buf[1] == 'A' &&
  190. buf[2] == 'G'))
  191. return -1;
  192. id3_get_string(s->title, sizeof(s->title), buf + 3, 30);
  193. id3_get_string(s->author, sizeof(s->author), buf + 33, 30);
  194. id3_get_string(s->album, sizeof(s->album), buf + 63, 30);
  195. id3_get_string(str, sizeof(str), buf + 93, 4);
  196. s->year = atoi(str);
  197. id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
  198. if (buf[125] == 0 && buf[126] != 0)
  199. s->track = buf[126];
  200. genre = buf[127];
  201. if (genre <= ID3_GENRE_MAX)
  202. pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]);
  203. return 0;
  204. }
  205. static void id3_create_tag(AVFormatContext *s, uint8_t *buf)
  206. {
  207. int v, i;
  208. memset(buf, 0, ID3_TAG_SIZE); /* fail safe */
  209. buf[0] = 'T';
  210. buf[1] = 'A';
  211. buf[2] = 'G';
  212. strncpy(buf + 3, s->title, 30);
  213. strncpy(buf + 33, s->author, 30);
  214. strncpy(buf + 63, s->album, 30);
  215. v = s->year;
  216. if (v > 0) {
  217. for(i = 0;i < 4; i++) {
  218. buf[96 - i] = '0' + (v % 10);
  219. v = v / 10;
  220. }
  221. }
  222. strncpy(buf + 97, s->comment, 30);
  223. if (s->track != 0) {
  224. buf[125] = 0;
  225. buf[126] = s->track;
  226. }
  227. for(i = 0; i <= ID3_GENRE_MAX; i++) {
  228. if (!strcasecmp(s->genre, id3_genre_str[i])) {
  229. buf[127] = i;
  230. break;
  231. }
  232. }
  233. }
  234. /* mp3 read */
  235. static int mp3_read_probe(AVProbeData *p)
  236. {
  237. int max_frames, first_frames;
  238. int fsize, frames, sample_rate;
  239. uint32_t header;
  240. uint8_t *buf, *buf2, *end;
  241. AVCodecContext avctx;
  242. if(p->buf_size < ID3_HEADER_SIZE)
  243. return 0;
  244. if(id3_match(p->buf))
  245. return AVPROBE_SCORE_MAX/2+1; // this must be less then mpeg-ps because some retards put id3 tage before mpeg-ps files
  246. max_frames = 0;
  247. buf = p->buf;
  248. end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
  249. for(; buf < end; buf++) {
  250. buf2 = buf;
  251. for(frames = 0; buf2 < end; frames++) {
  252. header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3];
  253. fsize = mpa_decode_header(&avctx, header, &sample_rate);
  254. if(fsize < 0)
  255. break;
  256. buf2 += fsize;
  257. }
  258. max_frames = FFMAX(max_frames, frames);
  259. if(buf == p->buf)
  260. first_frames= frames;
  261. }
  262. if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
  263. else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
  264. else if(max_frames>=1) return 1;
  265. else return 0;
  266. }
  267. static int mp3_read_header(AVFormatContext *s,
  268. AVFormatParameters *ap)
  269. {
  270. AVStream *st;
  271. uint8_t buf[ID3_TAG_SIZE];
  272. int len, ret, filesize;
  273. st = av_new_stream(s, 0);
  274. if (!st)
  275. return AVERROR_NOMEM;
  276. st->codec->codec_type = CODEC_TYPE_AUDIO;
  277. st->codec->codec_id = CODEC_ID_MP3;
  278. st->need_parsing = 1;
  279. /* try to get the TAG */
  280. if (!url_is_streamed(&s->pb)) {
  281. /* XXX: change that */
  282. filesize = url_fsize(&s->pb);
  283. if (filesize > 128) {
  284. url_fseek(&s->pb, filesize - 128, SEEK_SET);
  285. ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
  286. if (ret == ID3_TAG_SIZE) {
  287. id3_parse_tag(s, buf);
  288. }
  289. url_fseek(&s->pb, 0, SEEK_SET);
  290. }
  291. }
  292. /* if ID3 header found, skip it */
  293. ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
  294. if (ret != ID3_HEADER_SIZE)
  295. return -1;
  296. if (id3_match(buf)) {
  297. /* skip ID3 header */
  298. len = ((buf[6] & 0x7f) << 21) |
  299. ((buf[7] & 0x7f) << 14) |
  300. ((buf[8] & 0x7f) << 7) |
  301. (buf[9] & 0x7f);
  302. url_fskip(&s->pb, len);
  303. } else {
  304. url_fseek(&s->pb, 0, SEEK_SET);
  305. }
  306. /* the parameters will be extracted from the compressed bitstream */
  307. return 0;
  308. }
  309. #define MP3_PACKET_SIZE 1024
  310. static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
  311. {
  312. int ret, size;
  313. // AVStream *st = s->streams[0];
  314. size= MP3_PACKET_SIZE;
  315. ret= av_get_packet(&s->pb, pkt, size);
  316. pkt->stream_index = 0;
  317. if (ret <= 0) {
  318. return AVERROR_IO;
  319. }
  320. /* note: we need to modify the packet size here to handle the last
  321. packet */
  322. pkt->size = ret;
  323. return ret;
  324. }
  325. static int mp3_read_close(AVFormatContext *s)
  326. {
  327. return 0;
  328. }
  329. #ifdef CONFIG_MUXERS
  330. /* simple formats */
  331. static int mp3_write_header(struct AVFormatContext *s)
  332. {
  333. return 0;
  334. }
  335. static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  336. {
  337. put_buffer(&s->pb, pkt->data, pkt->size);
  338. put_flush_packet(&s->pb);
  339. return 0;
  340. }
  341. static int mp3_write_trailer(struct AVFormatContext *s)
  342. {
  343. uint8_t buf[ID3_TAG_SIZE];
  344. /* write the id3 header */
  345. if (s->title[0] != '\0') {
  346. id3_create_tag(s, buf);
  347. put_buffer(&s->pb, buf, ID3_TAG_SIZE);
  348. put_flush_packet(&s->pb);
  349. }
  350. return 0;
  351. }
  352. #endif //CONFIG_MUXERS
  353. #ifdef CONFIG_MP3_DEMUXER
  354. AVInputFormat mp3_demuxer = {
  355. "mp3",
  356. "MPEG audio",
  357. 0,
  358. mp3_read_probe,
  359. mp3_read_header,
  360. mp3_read_packet,
  361. mp3_read_close,
  362. .flags= AVFMT_GENERIC_INDEX,
  363. .extensions = "mp2,mp3,m2a", /* XXX: use probe */
  364. };
  365. #endif
  366. #ifdef CONFIG_MP2_MUXER
  367. AVOutputFormat mp2_muxer = {
  368. "mp2",
  369. "MPEG audio layer 2",
  370. "audio/x-mpeg",
  371. #ifdef CONFIG_LIBMP3LAME
  372. "mp2,m2a",
  373. #else
  374. "mp2,mp3,m2a",
  375. #endif
  376. 0,
  377. CODEC_ID_MP2,
  378. 0,
  379. mp3_write_header,
  380. mp3_write_packet,
  381. mp3_write_trailer,
  382. };
  383. #endif
  384. #ifdef CONFIG_MP3_MUXER
  385. AVOutputFormat mp3_muxer = {
  386. "mp3",
  387. "MPEG audio layer 3",
  388. "audio/x-mpeg",
  389. "mp3",
  390. 0,
  391. CODEC_ID_MP3,
  392. 0,
  393. mp3_write_header,
  394. mp3_write_packet,
  395. mp3_write_trailer,
  396. };
  397. #endif