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.

427 lines
10KB

  1. /*
  2. * MP3 encoder and decoder
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "mpegaudio.h"
  21. #define ID3_HEADER_SIZE 10
  22. #define ID3_TAG_SIZE 128
  23. #define ID3_GENRE_MAX 125
  24. static const char *id3_genre_str[ID3_GENRE_MAX + 1] = {
  25. [0] = "Blues",
  26. [1] = "Classic Rock",
  27. [2] = "Country",
  28. [3] = "Dance",
  29. [4] = "Disco",
  30. [5] = "Funk",
  31. [6] = "Grunge",
  32. [7] = "Hip-Hop",
  33. [8] = "Jazz",
  34. [9] = "Metal",
  35. [10] = "New Age",
  36. [11] = "Oldies",
  37. [12] = "Other",
  38. [13] = "Pop",
  39. [14] = "R&B",
  40. [15] = "Rap",
  41. [16] = "Reggae",
  42. [17] = "Rock",
  43. [18] = "Techno",
  44. [19] = "Industrial",
  45. [20] = "Alternative",
  46. [21] = "Ska",
  47. [22] = "Death Metal",
  48. [23] = "Pranks",
  49. [24] = "Soundtrack",
  50. [25] = "Euro-Techno",
  51. [26] = "Ambient",
  52. [27] = "Trip-Hop",
  53. [28] = "Vocal",
  54. [29] = "Jazz+Funk",
  55. [30] = "Fusion",
  56. [31] = "Trance",
  57. [32] = "Classical",
  58. [33] = "Instrumental",
  59. [34] = "Acid",
  60. [35] = "House",
  61. [36] = "Game",
  62. [37] = "Sound Clip",
  63. [38] = "Gospel",
  64. [39] = "Noise",
  65. [40] = "AlternRock",
  66. [41] = "Bass",
  67. [42] = "Soul",
  68. [43] = "Punk",
  69. [44] = "Space",
  70. [45] = "Meditative",
  71. [46] = "Instrumental Pop",
  72. [47] = "Instrumental Rock",
  73. [48] = "Ethnic",
  74. [49] = "Gothic",
  75. [50] = "Darkwave",
  76. [51] = "Techno-Industrial",
  77. [52] = "Electronic",
  78. [53] = "Pop-Folk",
  79. [54] = "Eurodance",
  80. [55] = "Dream",
  81. [56] = "Southern Rock",
  82. [57] = "Comedy",
  83. [58] = "Cult",
  84. [59] = "Gangsta",
  85. [60] = "Top 40",
  86. [61] = "Christian Rap",
  87. [62] = "Pop/Funk",
  88. [63] = "Jungle",
  89. [64] = "Native American",
  90. [65] = "Cabaret",
  91. [66] = "New Wave",
  92. [67] = "Psychadelic",
  93. [68] = "Rave",
  94. [69] = "Showtunes",
  95. [70] = "Trailer",
  96. [71] = "Lo-Fi",
  97. [72] = "Tribal",
  98. [73] = "Acid Punk",
  99. [74] = "Acid Jazz",
  100. [75] = "Polka",
  101. [76] = "Retro",
  102. [77] = "Musical",
  103. [78] = "Rock & Roll",
  104. [79] = "Hard Rock",
  105. [80] = "Folk",
  106. [81] = "Folk-Rock",
  107. [82] = "National Folk",
  108. [83] = "Swing",
  109. [84] = "Fast Fusion",
  110. [85] = "Bebob",
  111. [86] = "Latin",
  112. [87] = "Revival",
  113. [88] = "Celtic",
  114. [89] = "Bluegrass",
  115. [90] = "Avantgarde",
  116. [91] = "Gothic Rock",
  117. [92] = "Progressive Rock",
  118. [93] = "Psychedelic Rock",
  119. [94] = "Symphonic Rock",
  120. [95] = "Slow Rock",
  121. [96] = "Big Band",
  122. [97] = "Chorus",
  123. [98] = "Easy Listening",
  124. [99] = "Acoustic",
  125. [100] = "Humour",
  126. [101] = "Speech",
  127. [102] = "Chanson",
  128. [103] = "Opera",
  129. [104] = "Chamber Music",
  130. [105] = "Sonata",
  131. [106] = "Symphony",
  132. [107] = "Booty Bass",
  133. [108] = "Primus",
  134. [109] = "Porn Groove",
  135. [110] = "Satire",
  136. [111] = "Slow Jam",
  137. [112] = "Club",
  138. [113] = "Tango",
  139. [114] = "Samba",
  140. [115] = "Folklore",
  141. [116] = "Ballad",
  142. [117] = "Power Ballad",
  143. [118] = "Rhythmic Soul",
  144. [119] = "Freestyle",
  145. [120] = "Duet",
  146. [121] = "Punk Rock",
  147. [122] = "Drum Solo",
  148. [123] = "A capella",
  149. [124] = "Euro-House",
  150. [125] = "Dance Hall",
  151. };
  152. /* buf must be ID3_HEADER_SIZE byte long */
  153. static int id3_match(const uint8_t *buf)
  154. {
  155. return (buf[0] == 'I' &&
  156. buf[1] == 'D' &&
  157. buf[2] == '3' &&
  158. buf[3] != 0xff &&
  159. buf[4] != 0xff &&
  160. (buf[6] & 0x80) == 0 &&
  161. (buf[7] & 0x80) == 0 &&
  162. (buf[8] & 0x80) == 0 &&
  163. (buf[9] & 0x80) == 0);
  164. }
  165. static void id3_get_string(char *str, int str_size,
  166. const uint8_t *buf, int buf_size)
  167. {
  168. int i, c;
  169. char *q;
  170. q = str;
  171. for(i = 0; i < buf_size; i++) {
  172. c = buf[i];
  173. if (c == '\0')
  174. break;
  175. if ((q - str) >= str_size - 1)
  176. break;
  177. *q++ = c;
  178. }
  179. *q = '\0';
  180. }
  181. /* 'buf' must be ID3_TAG_SIZE byte long */
  182. static int id3_parse_tag(AVFormatContext *s, const uint8_t *buf)
  183. {
  184. char str[5];
  185. int genre;
  186. if (!(buf[0] == 'T' &&
  187. buf[1] == 'A' &&
  188. buf[2] == 'G'))
  189. return -1;
  190. id3_get_string(s->title, sizeof(s->title), buf + 3, 30);
  191. id3_get_string(s->author, sizeof(s->author), buf + 33, 30);
  192. id3_get_string(s->album, sizeof(s->album), buf + 63, 30);
  193. id3_get_string(str, sizeof(str), buf + 93, 4);
  194. s->year = atoi(str);
  195. id3_get_string(s->comment, sizeof(s->comment), buf + 97, 30);
  196. if (buf[125] == 0 && buf[126] != 0)
  197. s->track = buf[126];
  198. genre = buf[127];
  199. if (genre <= ID3_GENRE_MAX)
  200. pstrcpy(s->genre, sizeof(s->genre), id3_genre_str[genre]);
  201. return 0;
  202. }
  203. static void id3_create_tag(AVFormatContext *s, uint8_t *buf)
  204. {
  205. int v, i;
  206. memset(buf, 0, ID3_TAG_SIZE); /* fail safe */
  207. buf[0] = 'T';
  208. buf[1] = 'A';
  209. buf[2] = 'G';
  210. strncpy(buf + 3, s->title, 30);
  211. strncpy(buf + 33, s->author, 30);
  212. strncpy(buf + 63, s->album, 30);
  213. v = s->year;
  214. if (v > 0) {
  215. for(i = 0;i < 4; i++) {
  216. buf[96 - i] = '0' + (v % 10);
  217. v = v / 10;
  218. }
  219. }
  220. strncpy(buf + 97, s->comment, 30);
  221. if (s->track != 0) {
  222. buf[125] = 0;
  223. buf[126] = s->track;
  224. }
  225. for(i = 0; i <= ID3_GENRE_MAX; i++) {
  226. if (!strcasecmp(s->genre, id3_genre_str[i])) {
  227. buf[127] = i;
  228. break;
  229. }
  230. }
  231. }
  232. /* mp3 read */
  233. static int mp3_read_probe(AVProbeData *p)
  234. {
  235. int max_frames;
  236. int fsize, frames;
  237. uint32_t header;
  238. uint8_t *buf, *buf2, *end;
  239. AVCodecContext avctx;
  240. if(p->buf_size < ID3_HEADER_SIZE)
  241. return 0;
  242. if(id3_match(p->buf))
  243. return AVPROBE_SCORE_MAX;
  244. max_frames = 0;
  245. buf = p->buf;
  246. end = buf + FFMIN(4096, p->buf_size - sizeof(uint32_t));
  247. for(; buf < end; buf++) {
  248. buf2 = buf;
  249. for(frames = 0; buf2 < end; frames++) {
  250. header = (buf2[0] << 24) | (buf2[1] << 16) | (buf2[2] << 8) | buf2[3];
  251. fsize = mpa_decode_header(&avctx, header);
  252. if(fsize < 0)
  253. break;
  254. buf2 += fsize;
  255. }
  256. max_frames = FFMAX(max_frames, frames);
  257. }
  258. if (max_frames>=3) return AVPROBE_SCORE_MAX/2+1;
  259. else if(max_frames==2) return AVPROBE_SCORE_MAX/4;
  260. else if(max_frames==1) return 1;
  261. else return 0;
  262. }
  263. static int mp3_read_header(AVFormatContext *s,
  264. AVFormatParameters *ap)
  265. {
  266. AVStream *st;
  267. uint8_t buf[ID3_TAG_SIZE];
  268. int len, ret, filesize;
  269. st = av_new_stream(s, 0);
  270. if (!st)
  271. return AVERROR_NOMEM;
  272. st->codec->codec_type = CODEC_TYPE_AUDIO;
  273. st->codec->codec_id = CODEC_ID_MP3;
  274. st->need_parsing = 1;
  275. /* try to get the TAG */
  276. if (!url_is_streamed(&s->pb)) {
  277. /* XXX: change that */
  278. filesize = url_fsize(&s->pb);
  279. if (filesize > 128) {
  280. url_fseek(&s->pb, filesize - 128, SEEK_SET);
  281. ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
  282. if (ret == ID3_TAG_SIZE) {
  283. id3_parse_tag(s, buf);
  284. }
  285. url_fseek(&s->pb, 0, SEEK_SET);
  286. }
  287. }
  288. /* if ID3 header found, skip it */
  289. ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
  290. if (ret != ID3_HEADER_SIZE)
  291. return -1;
  292. if (id3_match(buf)) {
  293. /* skip ID3 header */
  294. len = ((buf[6] & 0x7f) << 21) |
  295. ((buf[7] & 0x7f) << 14) |
  296. ((buf[8] & 0x7f) << 7) |
  297. (buf[9] & 0x7f);
  298. url_fskip(&s->pb, len);
  299. } else {
  300. url_fseek(&s->pb, 0, SEEK_SET);
  301. }
  302. /* the parameters will be extracted from the compressed bitstream */
  303. return 0;
  304. }
  305. #define MP3_PACKET_SIZE 1024
  306. static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
  307. {
  308. int ret, size;
  309. // AVStream *st = s->streams[0];
  310. size= MP3_PACKET_SIZE;
  311. ret= av_get_packet(&s->pb, pkt, size);
  312. pkt->stream_index = 0;
  313. if (ret <= 0) {
  314. return AVERROR_IO;
  315. }
  316. /* note: we need to modify the packet size here to handle the last
  317. packet */
  318. pkt->size = ret;
  319. return ret;
  320. }
  321. static int mp3_read_close(AVFormatContext *s)
  322. {
  323. return 0;
  324. }
  325. #ifdef CONFIG_MUXERS
  326. /* simple formats */
  327. static int mp3_write_header(struct AVFormatContext *s)
  328. {
  329. return 0;
  330. }
  331. static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  332. {
  333. put_buffer(&s->pb, pkt->data, pkt->size);
  334. put_flush_packet(&s->pb);
  335. return 0;
  336. }
  337. static int mp3_write_trailer(struct AVFormatContext *s)
  338. {
  339. uint8_t buf[ID3_TAG_SIZE];
  340. /* write the id3 header */
  341. if (s->title[0] != '\0') {
  342. id3_create_tag(s, buf);
  343. put_buffer(&s->pb, buf, ID3_TAG_SIZE);
  344. put_flush_packet(&s->pb);
  345. }
  346. return 0;
  347. }
  348. #endif //CONFIG_MUXERS
  349. #ifdef CONFIG_MP3_DEMUXER
  350. AVInputFormat mp3_demuxer = {
  351. "mp3",
  352. "MPEG audio",
  353. 0,
  354. mp3_read_probe,
  355. mp3_read_header,
  356. mp3_read_packet,
  357. mp3_read_close,
  358. .extensions = "mp2,mp3,m2a", /* XXX: use probe */
  359. };
  360. #endif
  361. #ifdef CONFIG_MP2_MUXER
  362. AVOutputFormat mp2_muxer = {
  363. "mp2",
  364. "MPEG audio layer 2",
  365. "audio/x-mpeg",
  366. #ifdef CONFIG_MP3LAME
  367. "mp2,m2a",
  368. #else
  369. "mp2,mp3,m2a",
  370. #endif
  371. 0,
  372. CODEC_ID_MP2,
  373. 0,
  374. mp3_write_header,
  375. mp3_write_packet,
  376. mp3_write_trailer,
  377. };
  378. #endif
  379. #ifdef CONFIG_MP3_MUXER
  380. AVOutputFormat mp3_muxer = {
  381. "mp3",
  382. "MPEG audio layer 3",
  383. "audio/x-mpeg",
  384. "mp3",
  385. 0,
  386. CODEC_ID_MP3,
  387. 0,
  388. mp3_write_header,
  389. mp3_write_packet,
  390. mp3_write_trailer,
  391. };
  392. #endif