mp3.c 19 KB

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