mp3.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 && genre <= ID3v1_GENRE_MAX)
  190. av_strlcpy(dst, id3v1_genre_str[genre], sizeof(dst));
  191. if (*dst)
  192. av_metadata_set(&s->metadata, key, dst);
  193. }
  194. /**
  195. * ID3v2 parser
  196. *
  197. * Handles ID3v2.2, 2.3 and 2.4.
  198. *
  199. */
  200. static void id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
  201. {
  202. int isv34, tlen;
  203. uint32_t tag;
  204. int64_t next;
  205. int taghdrlen;
  206. const char *reason;
  207. switch(version) {
  208. case 2:
  209. if(flags & 0x40) {
  210. reason = "compression";
  211. goto error;
  212. }
  213. isv34 = 0;
  214. taghdrlen = 6;
  215. break;
  216. case 3:
  217. case 4:
  218. isv34 = 1;
  219. taghdrlen = 10;
  220. break;
  221. default:
  222. reason = "version";
  223. goto error;
  224. }
  225. if(flags & 0x80) {
  226. reason = "unsynchronization";
  227. goto error;
  228. }
  229. if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
  230. int extlen = id3v2_get_size(s->pb, 4);
  231. if (version == 4)
  232. extlen -= 4; // in v2.4 the length includes the length field we just read
  233. if (extlen < 0) {
  234. reason = "invalid extended header length";
  235. goto error;
  236. }
  237. url_fskip(s->pb, extlen);
  238. }
  239. while(len >= taghdrlen) {
  240. if(isv34) {
  241. tag = get_be32(s->pb);
  242. tlen = id3v2_get_size(s->pb, 4);
  243. get_be16(s->pb); /* flags */
  244. } else {
  245. tag = get_be24(s->pb);
  246. tlen = id3v2_get_size(s->pb, 3);
  247. }
  248. len -= taghdrlen + tlen;
  249. if(len < 0)
  250. break;
  251. next = url_ftell(s->pb) + tlen;
  252. switch(tag) {
  253. case MKBETAG('T', 'I', 'T', '2'):
  254. case MKBETAG(0, 'T', 'T', '2'):
  255. id3v2_read_ttag(s, tlen, "title");
  256. break;
  257. case MKBETAG('T', 'P', 'E', '1'):
  258. case MKBETAG(0, 'T', 'P', '1'):
  259. id3v2_read_ttag(s, tlen, "author");
  260. break;
  261. case MKBETAG('T', 'A', 'L', 'B'):
  262. case MKBETAG(0, 'T', 'A', 'L'):
  263. id3v2_read_ttag(s, tlen, "album");
  264. break;
  265. case MKBETAG('T', 'C', 'O', 'N'):
  266. case MKBETAG(0, 'T', 'C', 'O'):
  267. id3v2_read_ttag(s, tlen, "genre");
  268. break;
  269. case MKBETAG('T', 'C', 'O', 'P'):
  270. case MKBETAG(0, 'T', 'C', 'R'):
  271. id3v2_read_ttag(s, tlen, "copyright");
  272. break;
  273. case MKBETAG('T', 'R', 'C', 'K'):
  274. case MKBETAG(0, 'T', 'R', 'K'):
  275. id3v2_read_ttag(s, tlen, "track");
  276. break;
  277. case 0:
  278. /* padding, skip to end */
  279. url_fskip(s->pb, len);
  280. len = 0;
  281. continue;
  282. }
  283. /* Skip to end of tag */
  284. url_fseek(s->pb, next, SEEK_SET);
  285. }
  286. if(version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  287. url_fskip(s->pb, 10);
  288. return;
  289. error:
  290. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  291. url_fskip(s->pb, len);
  292. }
  293. static void id3v1_get_string(AVFormatContext *s, const char *key,
  294. const uint8_t *buf, int buf_size)
  295. {
  296. int i, c;
  297. char *q, str[512];
  298. q = str;
  299. for(i = 0; i < buf_size; i++) {
  300. c = buf[i];
  301. if (c == '\0')
  302. break;
  303. if ((q - str) >= sizeof(str) - 1)
  304. break;
  305. *q++ = c;
  306. }
  307. *q = '\0';
  308. if (*str)
  309. av_metadata_set(&s->metadata, key, str);
  310. }
  311. /* 'buf' must be ID3v1_TAG_SIZE byte long */
  312. static int id3v1_parse_tag(AVFormatContext *s, const uint8_t *buf)
  313. {
  314. char str[5];
  315. int genre;
  316. if (!(buf[0] == 'T' &&
  317. buf[1] == 'A' &&
  318. buf[2] == 'G'))
  319. return -1;
  320. id3v1_get_string(s, "title", buf + 3, 30);
  321. id3v1_get_string(s, "author", buf + 33, 30);
  322. id3v1_get_string(s, "album", buf + 63, 30);
  323. id3v1_get_string(s, "year", buf + 93, 4);
  324. id3v1_get_string(s, "comment", buf + 97, 30);
  325. if (buf[125] == 0 && buf[126] != 0) {
  326. snprintf(str, sizeof(str), "%d", buf[126]);
  327. av_metadata_set(&s->metadata, "track", str);
  328. }
  329. genre = buf[127];
  330. if (genre <= ID3v1_GENRE_MAX)
  331. av_metadata_set(&s->metadata, "genre", id3v1_genre_str[genre]);
  332. return 0;
  333. }
  334. /* mp3 read */
  335. static int mp3_read_probe(AVProbeData *p)
  336. {
  337. int max_frames, first_frames = 0;
  338. int fsize, frames, sample_rate;
  339. uint32_t header;
  340. uint8_t *buf, *buf0, *buf2, *end;
  341. AVCodecContext avctx;
  342. buf0 = p->buf;
  343. if(ff_id3v2_match(buf0)) {
  344. buf0 += ff_id3v2_tag_len(buf0);
  345. }
  346. max_frames = 0;
  347. buf = buf0;
  348. end = p->buf + p->buf_size - sizeof(uint32_t);
  349. for(; buf < end; buf= buf2+1) {
  350. buf2 = buf;
  351. for(frames = 0; buf2 < end; frames++) {
  352. header = AV_RB32(buf2);
  353. fsize = ff_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
  354. if(fsize < 0)
  355. break;
  356. buf2 += fsize;
  357. }
  358. max_frames = FFMAX(max_frames, frames);
  359. if(buf == buf0)
  360. first_frames= frames;
  361. }
  362. if (first_frames>=3) return AVPROBE_SCORE_MAX/2+1;
  363. else if(max_frames>500)return AVPROBE_SCORE_MAX/2;
  364. else if(max_frames>=3) return AVPROBE_SCORE_MAX/4;
  365. else if(buf0!=p->buf) return AVPROBE_SCORE_MAX/4-1;
  366. else if(max_frames>=1) return 1;
  367. else return 0;
  368. }
  369. /**
  370. * Try to find Xing/Info/VBRI tags and compute duration from info therein
  371. */
  372. static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
  373. {
  374. uint32_t v, spf;
  375. int frames = -1; /* Total number of frames in file */
  376. const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
  377. MPADecodeHeader c;
  378. int vbrtag_size = 0;
  379. v = get_be32(s->pb);
  380. if(ff_mpa_check_header(v) < 0)
  381. return -1;
  382. if (ff_mpegaudio_decode_header(&c, v) == 0)
  383. vbrtag_size = c.frame_size;
  384. if(c.layer != 3)
  385. return -1;
  386. /* Check for Xing / Info tag */
  387. url_fseek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
  388. v = get_be32(s->pb);
  389. if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
  390. v = get_be32(s->pb);
  391. if(v & 0x1)
  392. frames = get_be32(s->pb);
  393. }
  394. /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
  395. url_fseek(s->pb, base + 4 + 32, SEEK_SET);
  396. v = get_be32(s->pb);
  397. if(v == MKBETAG('V', 'B', 'R', 'I')) {
  398. /* Check tag version */
  399. if(get_be16(s->pb) == 1) {
  400. /* skip delay, quality and total bytes */
  401. url_fseek(s->pb, 8, SEEK_CUR);
  402. frames = get_be32(s->pb);
  403. }
  404. }
  405. if(frames < 0)
  406. return -1;
  407. /* Skip the vbr tag frame */
  408. url_fseek(s->pb, base + vbrtag_size, SEEK_SET);
  409. spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
  410. st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
  411. st->time_base);
  412. return 0;
  413. }
  414. static int mp3_read_header(AVFormatContext *s,
  415. AVFormatParameters *ap)
  416. {
  417. AVStream *st;
  418. uint8_t buf[ID3v1_TAG_SIZE];
  419. int len, ret, filesize;
  420. int64_t off;
  421. st = av_new_stream(s, 0);
  422. if (!st)
  423. return AVERROR(ENOMEM);
  424. st->codec->codec_type = CODEC_TYPE_AUDIO;
  425. st->codec->codec_id = CODEC_ID_MP3;
  426. st->need_parsing = AVSTREAM_PARSE_FULL;
  427. st->start_time = 0;
  428. /* try to get the TAG */
  429. if (!url_is_streamed(s->pb)) {
  430. /* XXX: change that */
  431. filesize = url_fsize(s->pb);
  432. if (filesize > 128) {
  433. url_fseek(s->pb, filesize - 128, SEEK_SET);
  434. ret = get_buffer(s->pb, buf, ID3v1_TAG_SIZE);
  435. if (ret == ID3v1_TAG_SIZE) {
  436. id3v1_parse_tag(s, buf);
  437. }
  438. url_fseek(s->pb, 0, SEEK_SET);
  439. }
  440. }
  441. /* if ID3v2 header found, skip it */
  442. ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
  443. if (ret != ID3v2_HEADER_SIZE)
  444. return -1;
  445. if (ff_id3v2_match(buf)) {
  446. /* parse ID3v2 header */
  447. len = ((buf[6] & 0x7f) << 21) |
  448. ((buf[7] & 0x7f) << 14) |
  449. ((buf[8] & 0x7f) << 7) |
  450. (buf[9] & 0x7f);
  451. id3v2_parse(s, len, buf[3], buf[5]);
  452. } else {
  453. url_fseek(s->pb, 0, SEEK_SET);
  454. }
  455. off = url_ftell(s->pb);
  456. if (mp3_parse_vbr_tags(s, st, off) < 0)
  457. url_fseek(s->pb, off, SEEK_SET);
  458. /* the parameters will be extracted from the compressed bitstream */
  459. return 0;
  460. }
  461. #define MP3_PACKET_SIZE 1024
  462. static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
  463. {
  464. int ret, size;
  465. // AVStream *st = s->streams[0];
  466. size= MP3_PACKET_SIZE;
  467. ret= av_get_packet(s->pb, pkt, size);
  468. pkt->stream_index = 0;
  469. if (ret <= 0) {
  470. return AVERROR(EIO);
  471. }
  472. /* note: we need to modify the packet size here to handle the last
  473. packet */
  474. pkt->size = ret;
  475. return ret;
  476. }
  477. #if CONFIG_MP2_MUXER || CONFIG_MP3_MUXER
  478. static int id3v1_set_string(AVFormatContext *s, const char *key,
  479. uint8_t *buf, int buf_size)
  480. {
  481. AVMetadataTag *tag;
  482. if ((tag = av_metadata_get(s->metadata, key, NULL, 0)))
  483. strncpy(buf, tag->value, buf_size);
  484. return !!tag;
  485. }
  486. static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
  487. {
  488. AVMetadataTag *tag;
  489. int i, count = 0;
  490. memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
  491. buf[0] = 'T';
  492. buf[1] = 'A';
  493. buf[2] = 'G';
  494. count += id3v1_set_string(s, "title", buf + 3, 30);
  495. count += id3v1_set_string(s, "author", buf + 33, 30);
  496. count += id3v1_set_string(s, "album", buf + 63, 30);
  497. count += id3v1_set_string(s, "year", buf + 93, 4);
  498. count += id3v1_set_string(s, "comment", buf + 97, 30);
  499. if ((tag = av_metadata_get(s->metadata, "track", NULL, 0))) {
  500. buf[125] = 0;
  501. buf[126] = atoi(tag->value);
  502. count++;
  503. }
  504. if ((tag = av_metadata_get(s->metadata, "genre", NULL, 0))) {
  505. for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
  506. if (!strcasecmp(tag->value, id3v1_genre_str[i])) {
  507. buf[127] = i;
  508. count++;
  509. break;
  510. }
  511. }
  512. }
  513. return count;
  514. }
  515. /* simple formats */
  516. static void id3v2_put_size(AVFormatContext *s, int size)
  517. {
  518. put_byte(s->pb, size >> 21 & 0x7f);
  519. put_byte(s->pb, size >> 14 & 0x7f);
  520. put_byte(s->pb, size >> 7 & 0x7f);
  521. put_byte(s->pb, size & 0x7f);
  522. }
  523. static void id3v2_put_ttag(AVFormatContext *s, const char *string, uint32_t tag)
  524. {
  525. int len = strlen(string);
  526. put_be32(s->pb, tag);
  527. id3v2_put_size(s, len + 1);
  528. put_be16(s->pb, 0);
  529. put_byte(s->pb, 3); /* UTF-8 */
  530. put_buffer(s->pb, string, len);
  531. }
  532. /**
  533. * Write an ID3v2.4 header at beginning of stream
  534. */
  535. static int mp3_write_header(struct AVFormatContext *s)
  536. {
  537. AVMetadataTag *title, *author, *album, *genre, *copyright, *track, *year;
  538. int totlen = 0;
  539. title = av_metadata_get(s->metadata, "title", NULL, 0);
  540. author = av_metadata_get(s->metadata, "author", NULL, 0);
  541. album = av_metadata_get(s->metadata, "album", NULL, 0);
  542. genre = av_metadata_get(s->metadata, "genre", NULL, 0);
  543. copyright = av_metadata_get(s->metadata, "copyright", NULL, 0);
  544. track = av_metadata_get(s->metadata, "track", NULL, 0);
  545. year = av_metadata_get(s->metadata, "year", NULL, 0);
  546. if(title) totlen += 11 + strlen(title->value);
  547. if(author) totlen += 11 + strlen(author->value);
  548. if(album) totlen += 11 + strlen(album->value);
  549. if(genre) totlen += 11 + strlen(genre->value);
  550. if(copyright) totlen += 11 + strlen(copyright->value);
  551. if(track) totlen += 11 + strlen(track->value);
  552. if(year) totlen += 11 + strlen(year->value);
  553. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  554. totlen += strlen(LIBAVFORMAT_IDENT) + 11;
  555. if(totlen == 0)
  556. return 0;
  557. put_be32(s->pb, MKBETAG('I', 'D', '3', 0x04)); /* ID3v2.4 */
  558. put_byte(s->pb, 0);
  559. put_byte(s->pb, 0); /* flags */
  560. id3v2_put_size(s, totlen);
  561. if(title) id3v2_put_ttag(s, title->value, MKBETAG('T', 'I', 'T', '2'));
  562. if(author) id3v2_put_ttag(s, author->value, MKBETAG('T', 'P', 'E', '1'));
  563. if(album) id3v2_put_ttag(s, album->value, MKBETAG('T', 'A', 'L', 'B'));
  564. if(genre) id3v2_put_ttag(s, genre->value, MKBETAG('T', 'C', 'O', 'N'));
  565. if(copyright) id3v2_put_ttag(s, copyright->value, MKBETAG('T', 'C', 'O', 'P'));
  566. if(track) id3v2_put_ttag(s, track->value, MKBETAG('T', 'R', 'C', 'K'));
  567. if(year) id3v2_put_ttag(s, year->value, MKBETAG('T', 'Y', 'E', 'R'));
  568. if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  569. id3v2_put_ttag(s, LIBAVFORMAT_IDENT, MKBETAG('T', 'E', 'N', 'C'));
  570. return 0;
  571. }
  572. static int mp3_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  573. {
  574. put_buffer(s->pb, pkt->data, pkt->size);
  575. put_flush_packet(s->pb);
  576. return 0;
  577. }
  578. static int mp3_write_trailer(struct AVFormatContext *s)
  579. {
  580. uint8_t buf[ID3v1_TAG_SIZE];
  581. /* write the id3v1 tag */
  582. if (id3v1_create_tag(s, buf) > 0) {
  583. put_buffer(s->pb, buf, ID3v1_TAG_SIZE);
  584. put_flush_packet(s->pb);
  585. }
  586. return 0;
  587. }
  588. #endif /* CONFIG_MP2_MUXER || CONFIG_MP3_MUXER */
  589. #if CONFIG_MP3_DEMUXER
  590. AVInputFormat mp3_demuxer = {
  591. "mp3",
  592. NULL_IF_CONFIG_SMALL("MPEG audio layer 2/3"),
  593. 0,
  594. mp3_read_probe,
  595. mp3_read_header,
  596. mp3_read_packet,
  597. .flags= AVFMT_GENERIC_INDEX,
  598. .extensions = "mp2,mp3,m2a", /* XXX: use probe */
  599. };
  600. #endif
  601. #if CONFIG_MP2_MUXER
  602. AVOutputFormat mp2_muxer = {
  603. "mp2",
  604. NULL_IF_CONFIG_SMALL("MPEG audio layer 2"),
  605. "audio/x-mpeg",
  606. "mp2,m2a",
  607. 0,
  608. CODEC_ID_MP2,
  609. CODEC_ID_NONE,
  610. NULL,
  611. mp3_write_packet,
  612. mp3_write_trailer,
  613. };
  614. #endif
  615. #if CONFIG_MP3_MUXER
  616. AVOutputFormat mp3_muxer = {
  617. "mp3",
  618. NULL_IF_CONFIG_SMALL("MPEG audio layer 3"),
  619. "audio/x-mpeg",
  620. "mp3",
  621. 0,
  622. CODEC_ID_MP3,
  623. CODEC_ID_NONE,
  624. mp3_write_header,
  625. mp3_write_packet,
  626. mp3_write_trailer,
  627. };
  628. #endif