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.

633 lines
20KB

  1. /*
  2. * MP3 muxer
  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 "avformat.h"
  22. #include "avio_internal.h"
  23. #include "id3v1.h"
  24. #include "id3v2.h"
  25. #include "rawenc.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavcodec/mpegaudio.h"
  28. #include "libavcodec/mpegaudiodata.h"
  29. #include "libavcodec/mpegaudiodecheader.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/dict.h"
  33. #include "libavutil/avassert.h"
  34. #include "libavutil/crc.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/replaygain.h"
  37. static int id3v1_set_string(AVFormatContext *s, const char *key,
  38. uint8_t *buf, int buf_size)
  39. {
  40. AVDictionaryEntry *tag;
  41. if ((tag = av_dict_get(s->metadata, key, NULL, 0)))
  42. av_strlcpy(buf, tag->value, buf_size);
  43. return !!tag;
  44. }
  45. static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
  46. {
  47. AVDictionaryEntry *tag;
  48. int i, count = 0;
  49. memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
  50. buf[0] = 'T';
  51. buf[1] = 'A';
  52. buf[2] = 'G';
  53. /* we knowingly overspecify each tag length by one byte to compensate for the mandatory null byte added by av_strlcpy */
  54. count += id3v1_set_string(s, "TIT2", buf + 3, 30 + 1); //title
  55. count += id3v1_set_string(s, "TPE1", buf + 33, 30 + 1); //author|artist
  56. count += id3v1_set_string(s, "TALB", buf + 63, 30 + 1); //album
  57. count += id3v1_set_string(s, "TDRC", buf + 93, 4 + 1); //date
  58. count += id3v1_set_string(s, "comment", buf + 97, 30 + 1);
  59. if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
  60. buf[125] = 0;
  61. buf[126] = atoi(tag->value);
  62. count++;
  63. }
  64. buf[127] = 0xFF; /* default to unknown genre */
  65. if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) { //genre
  66. for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
  67. if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
  68. buf[127] = i;
  69. count++;
  70. break;
  71. }
  72. }
  73. }
  74. return count;
  75. }
  76. #define XING_NUM_BAGS 400
  77. #define XING_TOC_SIZE 100
  78. // size of the XING/LAME data, starting from the Xing tag
  79. #define XING_SIZE 156
  80. typedef struct MP3Context {
  81. const AVClass *class;
  82. ID3v2EncContext id3;
  83. int id3v2_version;
  84. int write_id3v1;
  85. int write_xing;
  86. /* xing header */
  87. // a buffer containing the whole XING/LAME frame
  88. uint8_t *xing_frame;
  89. int xing_frame_size;
  90. AVCRC audio_crc; // CRC of the audio data
  91. uint32_t audio_size; // total size of the audio data
  92. // offset of the XING/LAME frame in the file
  93. int64_t xing_frame_offset;
  94. // offset of the XING/INFO tag in the frame
  95. int xing_offset;
  96. int32_t frames;
  97. int32_t size;
  98. uint32_t want;
  99. uint32_t seen;
  100. uint32_t pos;
  101. uint64_t bag[XING_NUM_BAGS];
  102. int initial_bitrate;
  103. int has_variable_bitrate;
  104. /* index of the audio stream */
  105. int audio_stream_idx;
  106. /* number of attached pictures we still need to write */
  107. int pics_to_write;
  108. /* audio packets are queued here until we get all the attached pictures */
  109. AVPacketList *queue, *queue_end;
  110. } MP3Context;
  111. static const uint8_t xing_offtbl[2][2] = {{32, 17}, {17, 9}};
  112. /*
  113. * Write an empty XING header and initialize respective data.
  114. */
  115. static int mp3_write_xing(AVFormatContext *s)
  116. {
  117. MP3Context *mp3 = s->priv_data;
  118. AVCodecContext *codec = s->streams[mp3->audio_stream_idx]->codec;
  119. AVDictionaryEntry *enc = av_dict_get(s->streams[mp3->audio_stream_idx]->metadata, "encoder", NULL, 0);
  120. AVIOContext *dyn_ctx;
  121. int32_t header;
  122. MPADecodeHeader mpah;
  123. int srate_idx, i, channels;
  124. int bitrate_idx;
  125. int best_bitrate_idx = -1;
  126. int best_bitrate_error = INT_MAX;
  127. int ret;
  128. int ver = 0;
  129. int bytes_needed;
  130. if (!s->pb->seekable || !mp3->write_xing)
  131. return 0;
  132. for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++) {
  133. const uint16_t base_freq = avpriv_mpa_freq_tab[i];
  134. if (codec->sample_rate == base_freq) ver = 0x3; // MPEG 1
  135. else if (codec->sample_rate == base_freq / 2) ver = 0x2; // MPEG 2
  136. else if (codec->sample_rate == base_freq / 4) ver = 0x0; // MPEG 2.5
  137. else continue;
  138. srate_idx = i;
  139. break;
  140. }
  141. if (i == FF_ARRAY_ELEMS(avpriv_mpa_freq_tab)) {
  142. av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");
  143. return -1;
  144. }
  145. switch (codec->channels) {
  146. case 1: channels = MPA_MONO; break;
  147. case 2: channels = MPA_STEREO; break;
  148. default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, "
  149. "not writing Xing header.\n");
  150. return -1;
  151. }
  152. /* dummy MPEG audio header */
  153. header = 0xffU << 24; // sync
  154. header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16; // sync/audio-version/layer 3/no crc*/
  155. header |= (srate_idx << 2) << 8;
  156. header |= channels << 6;
  157. for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) {
  158. int bit_rate = 1000 * avpriv_mpa_bitrate_tab[ver != 3][3 - 1][bitrate_idx];
  159. int error = FFABS(bit_rate - codec->bit_rate);
  160. if (error < best_bitrate_error) {
  161. best_bitrate_error = error;
  162. best_bitrate_idx = bitrate_idx;
  163. }
  164. }
  165. av_assert0(best_bitrate_idx >= 0);
  166. for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) {
  167. int32_t mask = bitrate_idx << (4 + 8);
  168. if (15 == bitrate_idx)
  169. return -1;
  170. header |= mask;
  171. avpriv_mpegaudio_decode_header(&mpah, header);
  172. mp3->xing_offset = xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1] + 4;
  173. bytes_needed = mp3->xing_offset + XING_SIZE;
  174. if (bytes_needed <= mpah.frame_size)
  175. break;
  176. header &= ~mask;
  177. }
  178. ret = avio_open_dyn_buf(&dyn_ctx);
  179. if (ret < 0)
  180. return ret;
  181. avio_wb32(dyn_ctx, header);
  182. ffio_fill(dyn_ctx, 0, mp3->xing_offset - 4);
  183. ffio_wfourcc(dyn_ctx, "Xing");
  184. avio_wb32(dyn_ctx, 0x01 | 0x02 | 0x04 | 0x08); // frames / size / TOC / vbr scale
  185. mp3->size = mpah.frame_size;
  186. mp3->want=1;
  187. mp3->seen=0;
  188. mp3->pos=0;
  189. avio_wb32(dyn_ctx, 0); // frames
  190. avio_wb32(dyn_ctx, 0); // size
  191. // TOC
  192. for (i = 0; i < XING_TOC_SIZE; i++)
  193. avio_w8(dyn_ctx, (uint8_t)(255 * i / XING_TOC_SIZE));
  194. // vbr quality
  195. // we write it, because some (broken) tools always expect it to be present
  196. avio_wb32(dyn_ctx, 0);
  197. // encoder short version string
  198. if (enc) {
  199. uint8_t encoder_str[9] = { 0 };
  200. if ( strlen(enc->value) > sizeof(encoder_str)
  201. && !strcmp("Lavc libmp3lame", enc->value)) {
  202. memcpy(encoder_str, "Lavf lame", 9);
  203. } else
  204. memcpy(encoder_str, enc->value, FFMIN(strlen(enc->value), sizeof(encoder_str)));
  205. avio_write(dyn_ctx, encoder_str, sizeof(encoder_str));
  206. } else
  207. avio_write(dyn_ctx, "Lavf\0\0\0\0\0", 9);
  208. avio_w8(dyn_ctx, 0); // tag revision 0 / unknown vbr method
  209. avio_w8(dyn_ctx, 0); // unknown lowpass filter value
  210. ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields
  211. avio_w8(dyn_ctx, 0); // unknown encoding flags
  212. avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate
  213. // encoder delay
  214. if (codec->initial_padding - 528 - 1 >= 1 << 12) {
  215. av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
  216. }
  217. avio_wb24(dyn_ctx, FFMAX(codec->initial_padding - 528 - 1, 0)<<12);
  218. avio_w8(dyn_ctx, 0); // misc
  219. avio_w8(dyn_ctx, 0); // mp3gain
  220. avio_wb16(dyn_ctx, 0); // preset
  221. // audio length and CRCs (will be updated later)
  222. avio_wb32(dyn_ctx, 0); // music length
  223. avio_wb16(dyn_ctx, 0); // music crc
  224. avio_wb16(dyn_ctx, 0); // tag crc
  225. ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed);
  226. mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame);
  227. mp3->xing_frame_offset = avio_tell(s->pb);
  228. avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
  229. mp3->audio_size = mp3->xing_frame_size;
  230. return 0;
  231. }
  232. /*
  233. * Add a frame to XING data.
  234. * Following lame's "VbrTag.c".
  235. */
  236. static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
  237. {
  238. int i;
  239. mp3->frames++;
  240. mp3->seen++;
  241. mp3->size += pkt->size;
  242. if (mp3->want == mp3->seen) {
  243. mp3->bag[mp3->pos] = mp3->size;
  244. if (XING_NUM_BAGS == ++mp3->pos) {
  245. /* shrink table to half size by throwing away each second bag. */
  246. for (i = 1; i < XING_NUM_BAGS; i += 2)
  247. mp3->bag[i >> 1] = mp3->bag[i];
  248. /* double wanted amount per bag. */
  249. mp3->want *= 2;
  250. /* adjust current position to half of table size. */
  251. mp3->pos = XING_NUM_BAGS / 2;
  252. }
  253. mp3->seen = 0;
  254. }
  255. }
  256. static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
  257. {
  258. MP3Context *mp3 = s->priv_data;
  259. if (pkt->data && pkt->size >= 4) {
  260. MPADecodeHeader mpah;
  261. int ret;
  262. int av_unused base;
  263. uint32_t h;
  264. h = AV_RB32(pkt->data);
  265. ret = avpriv_mpegaudio_decode_header(&mpah, h);
  266. if (ret >= 0) {
  267. if (!mp3->initial_bitrate)
  268. mp3->initial_bitrate = mpah.bit_rate;
  269. if ((mpah.bit_rate == 0) || (mp3->initial_bitrate != mpah.bit_rate))
  270. mp3->has_variable_bitrate = 1;
  271. } else {
  272. av_log(s, AV_LOG_WARNING, "Audio packet of size %d (starting with %08X...) "
  273. "is invalid, writing it anyway.\n", pkt->size, h);
  274. }
  275. #ifdef FILTER_VBR_HEADERS
  276. /* filter out XING and INFO headers. */
  277. base = 4 + xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1];
  278. if (base + 4 <= pkt->size) {
  279. uint32_t v = AV_RB32(pkt->data + base);
  280. if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v)
  281. return 0;
  282. }
  283. /* filter out VBRI headers. */
  284. base = 4 + 32;
  285. if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base))
  286. return 0;
  287. #endif
  288. if (mp3->xing_offset) {
  289. mp3_xing_add_frame(mp3, pkt);
  290. mp3->audio_size += pkt->size;
  291. mp3->audio_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE),
  292. mp3->audio_crc, pkt->data, pkt->size);
  293. }
  294. }
  295. return ff_raw_write_packet(s, pkt);
  296. }
  297. static int mp3_queue_flush(AVFormatContext *s)
  298. {
  299. MP3Context *mp3 = s->priv_data;
  300. AVPacketList *pktl;
  301. int ret = 0, write = 1;
  302. ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
  303. mp3_write_xing(s);
  304. while ((pktl = mp3->queue)) {
  305. if (write && (ret = mp3_write_audio_packet(s, &pktl->pkt)) < 0)
  306. write = 0;
  307. av_packet_unref(&pktl->pkt);
  308. mp3->queue = pktl->next;
  309. av_freep(&pktl);
  310. }
  311. mp3->queue_end = NULL;
  312. return ret;
  313. }
  314. static void mp3_update_xing(AVFormatContext *s)
  315. {
  316. MP3Context *mp3 = s->priv_data;
  317. AVReplayGain *rg;
  318. uint16_t tag_crc;
  319. uint8_t *toc;
  320. int i, rg_size;
  321. /* replace "Xing" identification string with "Info" for CBR files. */
  322. if (!mp3->has_variable_bitrate)
  323. AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
  324. AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames);
  325. AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);
  326. toc = mp3->xing_frame + mp3->xing_offset + 16;
  327. toc[0] = 0; // first toc entry has to be zero.
  328. for (i = 1; i < XING_TOC_SIZE; ++i) {
  329. int j = i * mp3->pos / XING_TOC_SIZE;
  330. int seek_point = 256LL * mp3->bag[j] / mp3->size;
  331. toc[i] = FFMIN(seek_point, 255);
  332. }
  333. /* write replaygain */
  334. rg = (AVReplayGain*)av_stream_get_side_data(s->streams[0], AV_PKT_DATA_REPLAYGAIN,
  335. &rg_size);
  336. if (rg && rg_size >= sizeof(*rg)) {
  337. uint16_t val;
  338. AV_WB32(mp3->xing_frame + mp3->xing_offset + 131,
  339. av_rescale(rg->track_peak, 1 << 23, 100000));
  340. if (rg->track_gain != INT32_MIN) {
  341. val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1);
  342. val |= (rg->track_gain < 0) << 9;
  343. val |= 1 << 13;
  344. AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val);
  345. }
  346. if (rg->album_gain != INT32_MIN) {
  347. val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1);
  348. val |= (rg->album_gain < 0) << 9;
  349. val |= 1 << 14;
  350. AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val);
  351. }
  352. }
  353. AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
  354. AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
  355. tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190);
  356. AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc);
  357. avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET);
  358. avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
  359. avio_seek(s->pb, 0, SEEK_END);
  360. }
  361. static int mp3_write_trailer(struct AVFormatContext *s)
  362. {
  363. uint8_t buf[ID3v1_TAG_SIZE];
  364. MP3Context *mp3 = s->priv_data;
  365. if (mp3->pics_to_write) {
  366. av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
  367. "attached pictures.\n");
  368. mp3_queue_flush(s);
  369. }
  370. /* write the id3v1 tag */
  371. if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
  372. avio_write(s->pb, buf, ID3v1_TAG_SIZE);
  373. }
  374. if (mp3->xing_offset)
  375. mp3_update_xing(s);
  376. av_freep(&mp3->xing_frame);
  377. return 0;
  378. }
  379. static int query_codec(enum AVCodecID id, int std_compliance)
  380. {
  381. const CodecMime *cm= ff_id3v2_mime_tags;
  382. while(cm->id != AV_CODEC_ID_NONE) {
  383. if(id == cm->id)
  384. return MKTAG('A', 'P', 'I', 'C');
  385. cm++;
  386. }
  387. return -1;
  388. }
  389. #if CONFIG_MP2_MUXER
  390. AVOutputFormat ff_mp2_muxer = {
  391. .name = "mp2",
  392. .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
  393. .mime_type = "audio/mpeg",
  394. .extensions = "mp2,m2a,mpa",
  395. .audio_codec = AV_CODEC_ID_MP2,
  396. .video_codec = AV_CODEC_ID_NONE,
  397. .write_packet = ff_raw_write_packet,
  398. .flags = AVFMT_NOTIMESTAMPS,
  399. };
  400. #endif
  401. #if CONFIG_MP3_MUXER
  402. static const AVOption options[] = {
  403. { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
  404. offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM},
  405. { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
  406. offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  407. { "write_xing", "Write the Xing header containing file duration.",
  408. offsetof(MP3Context, write_xing), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  409. { NULL },
  410. };
  411. static const AVClass mp3_muxer_class = {
  412. .class_name = "MP3 muxer",
  413. .item_name = av_default_item_name,
  414. .option = options,
  415. .version = LIBAVUTIL_VERSION_INT,
  416. };
  417. static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
  418. {
  419. MP3Context *mp3 = s->priv_data;
  420. if (pkt->stream_index == mp3->audio_stream_idx) {
  421. if (mp3->pics_to_write) {
  422. /* buffer audio packets until we get all the pictures */
  423. AVPacketList *pktl = av_mallocz(sizeof(*pktl));
  424. int ret;
  425. if (!pktl) {
  426. av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n");
  427. mp3->pics_to_write = 0;
  428. mp3_queue_flush(s);
  429. return mp3_write_audio_packet(s, pkt);
  430. }
  431. ret = av_copy_packet(&pktl->pkt, pkt);
  432. if (ret < 0) {
  433. av_freep(&pktl);
  434. return ret;
  435. }
  436. if (mp3->queue_end)
  437. mp3->queue_end->next = pktl;
  438. else
  439. mp3->queue = pktl;
  440. mp3->queue_end = pktl;
  441. } else
  442. return mp3_write_audio_packet(s, pkt);
  443. } else {
  444. int ret;
  445. /* warn only once for each stream */
  446. if (s->streams[pkt->stream_index]->nb_frames == 1) {
  447. av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
  448. " ignoring.\n", pkt->stream_index);
  449. }
  450. if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
  451. return 0;
  452. if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
  453. return ret;
  454. mp3->pics_to_write--;
  455. /* flush the buffered audio packets */
  456. if (!mp3->pics_to_write &&
  457. (ret = mp3_queue_flush(s)) < 0)
  458. return ret;
  459. }
  460. return 0;
  461. }
  462. /**
  463. * Write an ID3v2 header at beginning of stream
  464. */
  465. static int mp3_write_header(struct AVFormatContext *s)
  466. {
  467. MP3Context *mp3 = s->priv_data;
  468. int ret, i;
  469. if (mp3->id3v2_version &&
  470. mp3->id3v2_version != 3 &&
  471. mp3->id3v2_version != 4) {
  472. av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only "
  473. "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version);
  474. return AVERROR(EINVAL);
  475. }
  476. /* check the streams -- we want exactly one audio and arbitrary number of
  477. * video (attached pictures) */
  478. mp3->audio_stream_idx = -1;
  479. for (i = 0; i < s->nb_streams; i++) {
  480. AVStream *st = s->streams[i];
  481. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  482. if (mp3->audio_stream_idx >= 0 || st->codec->codec_id != AV_CODEC_ID_MP3) {
  483. av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
  484. "audio stream is required.\n");
  485. return AVERROR(EINVAL);
  486. }
  487. mp3->audio_stream_idx = i;
  488. } else if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
  489. av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
  490. return AVERROR(EINVAL);
  491. }
  492. }
  493. if (mp3->audio_stream_idx < 0) {
  494. av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
  495. return AVERROR(EINVAL);
  496. }
  497. mp3->pics_to_write = s->nb_streams - 1;
  498. if (mp3->pics_to_write && !mp3->id3v2_version) {
  499. av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the "
  500. "ID3v2 header is disabled.\n");
  501. return AVERROR(EINVAL);
  502. }
  503. if (mp3->id3v2_version) {
  504. ff_id3v2_start(&mp3->id3, s->pb, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
  505. ret = ff_id3v2_write_metadata(s, &mp3->id3);
  506. if (ret < 0)
  507. return ret;
  508. }
  509. if (!mp3->pics_to_write) {
  510. if (mp3->id3v2_version)
  511. ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
  512. mp3_write_xing(s);
  513. }
  514. return 0;
  515. }
  516. AVOutputFormat ff_mp3_muxer = {
  517. .name = "mp3",
  518. .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
  519. .mime_type = "audio/mpeg",
  520. .extensions = "mp3",
  521. .priv_data_size = sizeof(MP3Context),
  522. .audio_codec = AV_CODEC_ID_MP3,
  523. .video_codec = AV_CODEC_ID_PNG,
  524. .write_header = mp3_write_header,
  525. .write_packet = mp3_write_packet,
  526. .write_trailer = mp3_write_trailer,
  527. .query_codec = query_codec,
  528. .flags = AVFMT_NOTIMESTAMPS,
  529. .priv_class = &mp3_muxer_class,
  530. };
  531. #endif