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.

405 lines
9.4KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_header(AVFormatContext *s,
  233. AVFormatParameters *ap)
  234. {
  235. AVStream *st;
  236. uint8_t buf[ID3_TAG_SIZE];
  237. int len, ret, filesize;
  238. st = av_new_stream(s, 0);
  239. if (!st)
  240. return AVERROR_NOMEM;
  241. st->codec.codec_type = CODEC_TYPE_AUDIO;
  242. st->codec.codec_id = CODEC_ID_MP3;
  243. st->need_parsing = 1;
  244. /* try to get the TAG */
  245. if (!url_is_streamed(&s->pb)) {
  246. /* XXX: change that */
  247. filesize = url_filesize(url_fileno(&s->pb));
  248. if (filesize > 128) {
  249. url_fseek(&s->pb, filesize - 128, SEEK_SET);
  250. ret = get_buffer(&s->pb, buf, ID3_TAG_SIZE);
  251. if (ret == ID3_TAG_SIZE) {
  252. id3_parse_tag(s, buf);
  253. }
  254. url_fseek(&s->pb, 0, SEEK_SET);
  255. }
  256. }
  257. /* if ID3 header found, skip it */
  258. ret = get_buffer(&s->pb, buf, ID3_HEADER_SIZE);
  259. if (ret != ID3_HEADER_SIZE)
  260. return -1;
  261. if (id3_match(buf)) {
  262. /* skip ID3 header */
  263. len = ((buf[6] & 0x7f) << 21) |
  264. ((buf[7] & 0x7f) << 14) |
  265. ((buf[8] & 0x7f) << 7) |
  266. (buf[9] & 0x7f);
  267. url_fskip(&s->pb, len);
  268. } else {
  269. url_fseek(&s->pb, 0, SEEK_SET);
  270. }
  271. /* the parameters will be extracted from the compressed bitstream */
  272. return 0;
  273. }
  274. #define MP3_PACKET_SIZE 1024
  275. static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
  276. {
  277. int ret, size;
  278. // AVStream *st = s->streams[0];
  279. size= MP3_PACKET_SIZE;
  280. if (av_new_packet(pkt, size) < 0)
  281. return -EIO;
  282. pkt->stream_index = 0;
  283. ret = get_buffer(&s->pb, pkt->data, size);
  284. if (ret <= 0) {
  285. av_free_packet(pkt);
  286. return -EIO;
  287. }
  288. /* note: we need to modify the packet size here to handle the last
  289. packet */
  290. pkt->size = ret;
  291. return ret;
  292. }
  293. static int mp3_read_close(AVFormatContext *s)
  294. {
  295. return 0;
  296. }
  297. #ifdef CONFIG_ENCODERS
  298. /* simple formats */
  299. static int mp3_write_header(struct AVFormatContext *s)
  300. {
  301. return 0;
  302. }
  303. static int mp3_write_packet(struct AVFormatContext *s, int stream_index,
  304. const uint8_t *buf, int size, int64_t pts)
  305. {
  306. put_buffer(&s->pb, buf, size);
  307. put_flush_packet(&s->pb);
  308. return 0;
  309. }
  310. static int mp3_write_trailer(struct AVFormatContext *s)
  311. {
  312. uint8_t buf[ID3_TAG_SIZE];
  313. /* write the id3 header */
  314. if (s->title[0] != '\0') {
  315. id3_create_tag(s, buf);
  316. put_buffer(&s->pb, buf, ID3_TAG_SIZE);
  317. put_flush_packet(&s->pb);
  318. }
  319. return 0;
  320. }
  321. #endif //CONFIG_ENCODERS
  322. AVInputFormat mp3_iformat = {
  323. "mp3",
  324. "MPEG audio",
  325. 0,
  326. NULL,
  327. mp3_read_header,
  328. mp3_read_packet,
  329. mp3_read_close,
  330. .extensions = "mp2,mp3", /* XXX: use probe */
  331. };
  332. #ifdef CONFIG_ENCODERS
  333. AVOutputFormat mp2_oformat = {
  334. "mp2",
  335. "MPEG audio layer 2",
  336. "audio/x-mpeg",
  337. #ifdef CONFIG_MP3LAME
  338. "mp2",
  339. #else
  340. "mp2,mp3",
  341. #endif
  342. 0,
  343. CODEC_ID_MP2,
  344. 0,
  345. mp3_write_header,
  346. mp3_write_packet,
  347. mp3_write_trailer,
  348. };
  349. #ifdef CONFIG_MP3LAME
  350. AVOutputFormat mp3_oformat = {
  351. "mp3",
  352. "MPEG audio layer 3",
  353. "audio/x-mpeg",
  354. "mp3",
  355. 0,
  356. CODEC_ID_MP3,
  357. 0,
  358. mp3_write_header,
  359. mp3_write_packet,
  360. mp3_write_trailer,
  361. };
  362. #endif
  363. #endif //CONFIG_ENCODERS
  364. int mp3_init(void)
  365. {
  366. av_register_input_format(&mp3_iformat);
  367. #ifdef CONFIG_ENCODERS
  368. av_register_output_format(&mp2_oformat);
  369. #ifdef CONFIG_MP3LAME
  370. av_register_output_format(&mp3_oformat);
  371. #endif
  372. #endif //CONFIG_ENCODERS
  373. return 0;
  374. }