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.

415 lines
9.5KB

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