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.

632 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 av_unused base;
  262. uint32_t h;
  263. h = AV_RB32(pkt->data);
  264. if (ff_mpa_check_header(h) == 0) {
  265. avpriv_mpegaudio_decode_header(&mpah, h);
  266. if (!mp3->initial_bitrate)
  267. mp3->initial_bitrate = mpah.bit_rate;
  268. if ((mpah.bit_rate == 0) || (mp3->initial_bitrate != mpah.bit_rate))
  269. mp3->has_variable_bitrate = 1;
  270. } else {
  271. av_log(s, AV_LOG_WARNING, "Audio packet of size %d (starting with %08X...) "
  272. "is invalid, writing it anyway.\n", pkt->size, h);
  273. }
  274. #ifdef FILTER_VBR_HEADERS
  275. /* filter out XING and INFO headers. */
  276. base = 4 + xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1];
  277. if (base + 4 <= pkt->size) {
  278. uint32_t v = AV_RB32(pkt->data + base);
  279. if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v)
  280. return 0;
  281. }
  282. /* filter out VBRI headers. */
  283. base = 4 + 32;
  284. if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base))
  285. return 0;
  286. #endif
  287. if (mp3->xing_offset) {
  288. mp3_xing_add_frame(mp3, pkt);
  289. mp3->audio_size += pkt->size;
  290. mp3->audio_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE),
  291. mp3->audio_crc, pkt->data, pkt->size);
  292. }
  293. }
  294. return ff_raw_write_packet(s, pkt);
  295. }
  296. static int mp3_queue_flush(AVFormatContext *s)
  297. {
  298. MP3Context *mp3 = s->priv_data;
  299. AVPacketList *pktl;
  300. int ret = 0, write = 1;
  301. ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
  302. mp3_write_xing(s);
  303. while ((pktl = mp3->queue)) {
  304. if (write && (ret = mp3_write_audio_packet(s, &pktl->pkt)) < 0)
  305. write = 0;
  306. av_free_packet(&pktl->pkt);
  307. mp3->queue = pktl->next;
  308. av_freep(&pktl);
  309. }
  310. mp3->queue_end = NULL;
  311. return ret;
  312. }
  313. static void mp3_update_xing(AVFormatContext *s)
  314. {
  315. MP3Context *mp3 = s->priv_data;
  316. AVReplayGain *rg;
  317. uint16_t tag_crc;
  318. uint8_t *toc;
  319. int i, rg_size;
  320. /* replace "Xing" identification string with "Info" for CBR files. */
  321. if (!mp3->has_variable_bitrate)
  322. AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
  323. AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames);
  324. AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);
  325. toc = mp3->xing_frame + mp3->xing_offset + 16;
  326. toc[0] = 0; // first toc entry has to be zero.
  327. for (i = 1; i < XING_TOC_SIZE; ++i) {
  328. int j = i * mp3->pos / XING_TOC_SIZE;
  329. int seek_point = 256LL * mp3->bag[j] / mp3->size;
  330. toc[i] = FFMIN(seek_point, 255);
  331. }
  332. /* write replaygain */
  333. rg = (AVReplayGain*)av_stream_get_side_data(s->streams[0], AV_PKT_DATA_REPLAYGAIN,
  334. &rg_size);
  335. if (rg && rg_size >= sizeof(*rg)) {
  336. uint16_t val;
  337. AV_WB32(mp3->xing_frame + mp3->xing_offset + 131,
  338. av_rescale(rg->track_peak, 1 << 23, 100000));
  339. if (rg->track_gain != INT32_MIN) {
  340. val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1);
  341. val |= (rg->track_gain < 0) << 9;
  342. val |= 1 << 13;
  343. AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val);
  344. }
  345. if (rg->album_gain != INT32_MIN) {
  346. val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1);
  347. val |= (rg->album_gain < 0) << 9;
  348. val |= 1 << 14;
  349. AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val);
  350. }
  351. }
  352. AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
  353. AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
  354. tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190);
  355. AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc);
  356. avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET);
  357. avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
  358. avio_seek(s->pb, 0, SEEK_END);
  359. }
  360. static int mp3_write_trailer(struct AVFormatContext *s)
  361. {
  362. uint8_t buf[ID3v1_TAG_SIZE];
  363. MP3Context *mp3 = s->priv_data;
  364. if (mp3->pics_to_write) {
  365. av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
  366. "attached pictures.\n");
  367. mp3_queue_flush(s);
  368. }
  369. /* write the id3v1 tag */
  370. if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
  371. avio_write(s->pb, buf, ID3v1_TAG_SIZE);
  372. }
  373. if (mp3->xing_offset)
  374. mp3_update_xing(s);
  375. av_freep(&mp3->xing_frame);
  376. return 0;
  377. }
  378. static int query_codec(enum AVCodecID id, int std_compliance)
  379. {
  380. const CodecMime *cm= ff_id3v2_mime_tags;
  381. while(cm->id != AV_CODEC_ID_NONE) {
  382. if(id == cm->id)
  383. return MKTAG('A', 'P', 'I', 'C');
  384. cm++;
  385. }
  386. return -1;
  387. }
  388. #if CONFIG_MP2_MUXER
  389. AVOutputFormat ff_mp2_muxer = {
  390. .name = "mp2",
  391. .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
  392. .mime_type = "audio/mpeg",
  393. .extensions = "mp2,m2a,mpa",
  394. .audio_codec = AV_CODEC_ID_MP2,
  395. .video_codec = AV_CODEC_ID_NONE,
  396. .write_packet = ff_raw_write_packet,
  397. .flags = AVFMT_NOTIMESTAMPS,
  398. };
  399. #endif
  400. #if CONFIG_MP3_MUXER
  401. static const AVOption options[] = {
  402. { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
  403. offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM},
  404. { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
  405. offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  406. { "write_xing", "Write the Xing header containing file duration.",
  407. offsetof(MP3Context, write_xing), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  408. { NULL },
  409. };
  410. static const AVClass mp3_muxer_class = {
  411. .class_name = "MP3 muxer",
  412. .item_name = av_default_item_name,
  413. .option = options,
  414. .version = LIBAVUTIL_VERSION_INT,
  415. };
  416. static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
  417. {
  418. MP3Context *mp3 = s->priv_data;
  419. if (pkt->stream_index == mp3->audio_stream_idx) {
  420. if (mp3->pics_to_write) {
  421. /* buffer audio packets until we get all the pictures */
  422. AVPacketList *pktl = av_mallocz(sizeof(*pktl));
  423. int ret;
  424. if (!pktl) {
  425. av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n");
  426. mp3->pics_to_write = 0;
  427. mp3_queue_flush(s);
  428. return mp3_write_audio_packet(s, pkt);
  429. }
  430. ret = av_copy_packet(&pktl->pkt, pkt);
  431. if (ret < 0) {
  432. av_freep(&pktl);
  433. return ret;
  434. }
  435. if (mp3->queue_end)
  436. mp3->queue_end->next = pktl;
  437. else
  438. mp3->queue = pktl;
  439. mp3->queue_end = pktl;
  440. } else
  441. return mp3_write_audio_packet(s, pkt);
  442. } else {
  443. int ret;
  444. /* warn only once for each stream */
  445. if (s->streams[pkt->stream_index]->nb_frames == 1) {
  446. av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
  447. " ignoring.\n", pkt->stream_index);
  448. }
  449. if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
  450. return 0;
  451. if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
  452. return ret;
  453. mp3->pics_to_write--;
  454. /* flush the buffered audio packets */
  455. if (!mp3->pics_to_write &&
  456. (ret = mp3_queue_flush(s)) < 0)
  457. return ret;
  458. }
  459. return 0;
  460. }
  461. /**
  462. * Write an ID3v2 header at beginning of stream
  463. */
  464. static int mp3_write_header(struct AVFormatContext *s)
  465. {
  466. MP3Context *mp3 = s->priv_data;
  467. int ret, i;
  468. if (mp3->id3v2_version &&
  469. mp3->id3v2_version != 3 &&
  470. mp3->id3v2_version != 4) {
  471. av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only "
  472. "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version);
  473. return AVERROR(EINVAL);
  474. }
  475. /* check the streams -- we want exactly one audio and arbitrary number of
  476. * video (attached pictures) */
  477. mp3->audio_stream_idx = -1;
  478. for (i = 0; i < s->nb_streams; i++) {
  479. AVStream *st = s->streams[i];
  480. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  481. if (mp3->audio_stream_idx >= 0 || st->codec->codec_id != AV_CODEC_ID_MP3) {
  482. av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
  483. "audio stream is required.\n");
  484. return AVERROR(EINVAL);
  485. }
  486. mp3->audio_stream_idx = i;
  487. } else if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO) {
  488. av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
  489. return AVERROR(EINVAL);
  490. }
  491. }
  492. if (mp3->audio_stream_idx < 0) {
  493. av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
  494. return AVERROR(EINVAL);
  495. }
  496. mp3->pics_to_write = s->nb_streams - 1;
  497. if (mp3->pics_to_write && !mp3->id3v2_version) {
  498. av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the "
  499. "ID3v2 header is disabled.\n");
  500. return AVERROR(EINVAL);
  501. }
  502. if (mp3->id3v2_version) {
  503. ff_id3v2_start(&mp3->id3, s->pb, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
  504. ret = ff_id3v2_write_metadata(s, &mp3->id3);
  505. if (ret < 0)
  506. return ret;
  507. }
  508. if (!mp3->pics_to_write) {
  509. if (mp3->id3v2_version)
  510. ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
  511. mp3_write_xing(s);
  512. }
  513. return 0;
  514. }
  515. AVOutputFormat ff_mp3_muxer = {
  516. .name = "mp3",
  517. .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
  518. .mime_type = "audio/mpeg",
  519. .extensions = "mp3",
  520. .priv_data_size = sizeof(MP3Context),
  521. .audio_codec = AV_CODEC_ID_MP3,
  522. .video_codec = AV_CODEC_ID_PNG,
  523. .write_header = mp3_write_header,
  524. .write_packet = mp3_write_packet,
  525. .write_trailer = mp3_write_trailer,
  526. .query_codec = query_codec,
  527. .flags = AVFMT_NOTIMESTAMPS,
  528. .priv_class = &mp3_muxer_class,
  529. };
  530. #endif