FFmpeg
Loading...
Searching...
No Matches
mp3enc.c
Go to the documentation of this file.
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
22#include "avformat.h"
23#include "avio_internal.h"
24#include "id3v1.h"
25#include "id3v2.h"
26#include "mux.h"
27#include "rawenc.h"
28#include "libavutil/avstring.h"
29#include "libavutil/mem.h"
33#include "packet_internal.h"
35#include "libavutil/opt.h"
36#include "libavutil/dict.h"
37#include "libavutil/avassert.h"
38#include "libavutil/crc.h"
41
42static int id3v1_set_string(AVFormatContext *s, const char *key,
43 uint8_t *buf, int buf_size)
44{
46 if ((tag = av_dict_get(s->metadata, key, NULL, 0)))
47 av_strlcpy(buf, tag->value, buf_size);
48 return !!tag;
49}
50
51// refer to: http://id3.org/ID3v1
52static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
53{
55 int i, count = 0;
56
57 memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
58 buf[0] = 'T';
59 buf[1] = 'A';
60 buf[2] = 'G';
61 /* we knowingly overspecify each tag length by one byte to compensate for the mandatory null byte added by av_strlcpy */
62 count += id3v1_set_string(s, "TIT2", buf + 3, 30 + 1); //title
63 count += id3v1_set_string(s, "TPE1", buf + 33, 30 + 1); //author|artist
64 count += id3v1_set_string(s, "TALB", buf + 63, 30 + 1); //album
65 if ((tag = av_dict_get(s->metadata, "TYER", NULL, 0))) { //year
66 av_strlcpy(buf + 93, tag->value, 4 + 1);
67 count++;
68 } else if ((tag = av_dict_get(s->metadata, "TDRC", NULL, 0))) {
69 av_strlcpy(buf + 93, tag->value, 4 + 1);
70 count++;
71 } else if ((tag = av_dict_get(s->metadata, "TDAT", NULL, 0))) {
72 av_strlcpy(buf + 93, tag->value, 4 + 1);
73 count++;
74 }
75
76 count += id3v1_set_string(s, "comment", buf + 97, 30 + 1);
77 if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
78 buf[125] = 0;
79 buf[126] = atoi(tag->value);
80 count++;
81 }
82 buf[127] = 0xFF; /* default to unknown genre */
83 if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) { //genre
84 for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
85 if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
86 buf[127] = i;
87 count++;
88 break;
89 }
90 }
91 }
92 return count;
93}
94
95#define XING_NUM_BAGS 400
96#define XING_TOC_SIZE 100
97// size of the XING/LAME data, starting from the Xing tag
98#define XING_SIZE 156
99
100typedef struct MP3Context {
101 const AVClass *class;
107
108 /* xing header */
109 // a buffer containing the whole XING/LAME frame
110 uint8_t *xing_frame;
112
113 AVCRC audio_crc; // CRC of the audio data
114 uint32_t audio_size; // total size of the audio data
115
116 // offset of the XING/LAME frame in the file
118 // offset of the XING/INFO tag in the frame
120
123 uint32_t want;
124 uint32_t seen;
125 uint32_t pos;
129 int delay;
131
132 /* index of the audio stream */
134 /* number of attached pictures we still need to write */
136
137 /* audio packets are queued here until we get all the attached pictures */
139} MP3Context;
140
141static const uint8_t xing_offtbl[2][2] = {{32, 17}, {17, 9}};
142
143/*
144 * Write an empty XING header and initialize respective data.
145 */
147{
148 MP3Context *mp3 = s->priv_data;
149 AVCodecParameters *par = s->streams[mp3->audio_stream_idx]->codecpar;
150 AVDictionaryEntry *enc = av_dict_get(s->streams[mp3->audio_stream_idx]->metadata, "encoder", NULL, 0);
151 AVIOContext *dyn_ctx;
153 MPADecodeHeader mpah;
154 int srate_idx, i, channels;
155 int bitrate_idx;
156 int best_bitrate_idx = -1;
157 int best_bitrate_error = INT_MAX;
158 int ret;
159 int ver = 0;
160 int bytes_needed;
161
162 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) || !mp3->write_xing)
163 return 0;
164
165 for (i = 0; i < FF_ARRAY_ELEMS(ff_mpa_freq_tab); i++) {
166 const uint16_t base_freq = ff_mpa_freq_tab[i];
167
168 if (par->sample_rate == base_freq) ver = 0x3; // MPEG 1
169 else if (par->sample_rate == base_freq / 2) ver = 0x2; // MPEG 2
170 else if (par->sample_rate == base_freq / 4) ver = 0x0; // MPEG 2.5
171 else continue;
172
173 srate_idx = i;
174 break;
175 }
177 av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");
178 return -1;
179 }
180
181 switch (par->ch_layout.nb_channels) {
182 case 1: channels = MPA_MONO; break;
183 case 2: channels = MPA_STEREO; break;
184 default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, "
185 "not writing Xing header.\n");
186 return -1;
187 }
188
189 /* dummy MPEG audio header */
190 header = 0xffU << 24; // sync
191 header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16; // sync/audio-version/layer 3/no crc*/
192 header |= (srate_idx << 2) << 8;
193 header |= channels << 6;
194
195 for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) {
196 int bit_rate = 1000 * ff_mpa_bitrate_tab[ver != 3][3 - 1][bitrate_idx];
197 int error = FFABS(bit_rate - par->bit_rate);
198
199 if (error < best_bitrate_error) {
200 best_bitrate_error = error;
201 best_bitrate_idx = bitrate_idx;
202 }
203 }
204 av_assert0(best_bitrate_idx >= 0);
205
206 for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) {
207 int32_t mask = bitrate_idx << (4 + 8);
208 if (15 == bitrate_idx)
209 return -1;
210 header |= mask;
211
213 av_assert0(ret >= 0);
214 mp3->xing_offset = xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1] + 4;
215 bytes_needed = mp3->xing_offset + XING_SIZE;
216
217 if (bytes_needed <= mpah.frame_size)
218 break;
219
220 header &= ~mask;
221 }
222
223 ret = avio_open_dyn_buf(&dyn_ctx);
224 if (ret < 0)
225 return ret;
226
227 avio_wb32(dyn_ctx, header);
228
229 ffio_fill(dyn_ctx, 0, mp3->xing_offset - 4);
230 ffio_wfourcc(dyn_ctx, "Xing");
231 avio_wb32(dyn_ctx, 0x01 | 0x02 | 0x04 | 0x08); // frames / size / TOC / vbr scale
232
233 mp3->size = mpah.frame_size;
234 mp3->want=1;
235 mp3->seen=0;
236 mp3->pos=0;
237
238 avio_wb32(dyn_ctx, 0); // frames
239 avio_wb32(dyn_ctx, 0); // size
240
241 // TOC
242 for (i = 0; i < XING_TOC_SIZE; i++)
243 avio_w8(dyn_ctx, (uint8_t)(255 * i / XING_TOC_SIZE));
244
245 // vbr quality
246 // we write it, because some (broken) tools always expect it to be present
247 avio_wb32(dyn_ctx, 0);
248
249 // encoder short version string
250 if (enc) {
251 uint8_t encoder_str[9] = { 0 };
252 if ( strlen(enc->value) > sizeof(encoder_str)
253 && !strcmp("Lavc libmp3lame", enc->value)) {
254 memcpy(encoder_str, "Lavf lame", 9);
255 } else
256 memcpy(encoder_str, enc->value, FFMIN(strlen(enc->value), sizeof(encoder_str)));
257
258 avio_write(dyn_ctx, encoder_str, sizeof(encoder_str));
259 } else
260 avio_write(dyn_ctx, "Lavf\0\0\0\0\0", 9);
261
262 avio_w8(dyn_ctx, 0); // tag revision 0 / unknown vbr method
263 avio_w8(dyn_ctx, 0); // unknown lowpass filter value
264 ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields
265 avio_w8(dyn_ctx, 0); // unknown encoding flags
266 avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate
267 avio_wb24(dyn_ctx, 0); // empty encoder delay/padding
268
269 avio_w8(dyn_ctx, 0); // misc
270 avio_w8(dyn_ctx, 0); // mp3gain
271 avio_wb16(dyn_ctx, 0); // preset
272
273 // audio length and CRCs (will be updated later)
274 avio_wb32(dyn_ctx, 0); // music length
275 avio_wb16(dyn_ctx, 0); // music crc
276 avio_wb16(dyn_ctx, 0); // tag crc
277
278 ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed);
279
280 mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame);
281 mp3->xing_frame_offset = avio_tell(s->pb);
282 avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
283
284 mp3->audio_size = mp3->xing_frame_size;
285
286 return 0;
287}
288
289/*
290 * Add a frame to XING data.
291 * Following lame's "VbrTag.c".
292 */
294{
295 int i;
296
297 mp3->frames++;
298 mp3->seen++;
299 mp3->size += pkt->size;
300
301 if (mp3->want == mp3->seen) {
302 mp3->bag[mp3->pos] = mp3->size;
303
304 if (XING_NUM_BAGS == ++mp3->pos) {
305 /* shrink table to half size by throwing away each second bag. */
306 for (i = 1; i < XING_NUM_BAGS; i += 2)
307 mp3->bag[i >> 1] = mp3->bag[i];
308
309 /* double wanted amount per bag. */
310 mp3->want *= 2;
311 /* adjust current position to half of table size. */
312 mp3->pos = XING_NUM_BAGS / 2;
313 }
314
315 mp3->seen = 0;
316 }
317}
318
320{
321 MP3Context *mp3 = s->priv_data;
322
323 if (pkt->data && pkt->size >= 4) {
324 MPADecodeHeader mpah;
325 int ret;
326 uint32_t h;
327
328 h = AV_RB32(pkt->data);
329 ret = avpriv_mpegaudio_decode_header(&mpah, h);
330 if (ret >= 0) {
331 if (!mp3->initial_bitrate)
332 mp3->initial_bitrate = mpah.bit_rate;
333 if ((mpah.bit_rate == 0) || (mp3->initial_bitrate != mpah.bit_rate))
334 mp3->has_variable_bitrate = 1;
335 } else {
336 av_log(s, AV_LOG_WARNING, "Audio packet of size %d (starting with %08"PRIX32"...) "
337 "is invalid, writing it anyway.\n", pkt->size, h);
338 }
339
340#ifdef FILTER_VBR_HEADERS
341 /* filter out XING and INFO headers. */
342 int base = 4 + xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1];
343
344 if (base + 4 <= pkt->size) {
345 uint32_t v = AV_RB32(pkt->data + base);
346
347 if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v)
348 return 0;
349 }
350
351 /* filter out VBRI headers. */
352 base = 4 + 32;
353
354 if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base))
355 return 0;
356#endif
357
358 if (mp3->xing_offset) {
359 uint8_t *side_data = NULL;
360 size_t side_data_size;
361
363 mp3->audio_size += pkt->size;
365 mp3->audio_crc, pkt->data, pkt->size);
366
367 side_data = av_packet_get_side_data(pkt,
369 &side_data_size);
370 if (side_data && side_data_size >= 10) {
371 uint32_t discard_padding = AV_RL32(side_data + 4);
372 /* Padding longer than a frame is spread over several packets. */
373 mp3->padding = discard_padding ? mp3->padding + discard_padding : 0;
374 if (!mp3->delay)
375 mp3->delay = FFMAX((int64_t)AV_RL32(side_data) - 528 - 1, 0);
376 } else {
377 mp3->padding = 0;
378 }
379 }
380 }
381
382 return ff_raw_write_packet(s, pkt);
383}
384
386{
387 MP3Context *mp3 = s->priv_data;
388 AVIOContext *pb = mp3->id3_pb ? mp3->id3_pb : s->pb;
389 uint8_t *buf = NULL;
390 int ret, size;
391
392 ff_id3v2_finish(&mp3->id3, pb, s->metadata_header_padding);
393 ret = pb->error;
394
395 if (!mp3->id3_pb)
396 return ret;
397
398 size = avio_get_dyn_buf(mp3->id3_pb, &buf);
399 ret = mp3->id3_pb->error;
400 if (ret >= 0) {
401 avio_write(s->pb, buf, size);
402 ret = s->pb->error;
403 }
405
406 return ret;
407}
408
410{
411 MP3Context *mp3 = s->priv_data;
412 AVPacket *const pkt = ffformatcontext(s)->pkt;
413 int ret = 0, write = 1;
414
415 ret = mp3_finish_id3v2(s);
416 if (ret < 0)
417 write = 0;
418 else
420
421 while (mp3->queue.head) {
423 if (write && (ret = mp3_write_audio_packet(s, pkt)) < 0)
424 write = 0;
426 }
427 return ret;
428}
429
431{
432 MP3Context *mp3 = s->priv_data;
433 const AVPacketSideData *sd;
434 AVReplayGain *rg;
435 uint16_t tag_crc;
436 uint8_t *toc;
437 int i;
438 int64_t old_pos = avio_tell(s->pb);
439
440 /* replace "Xing" identification string with "Info" for CBR files. */
441 if (!mp3->has_variable_bitrate)
442 AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
443
444 AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames);
445 AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);
446
447 toc = mp3->xing_frame + mp3->xing_offset + 16;
448 toc[0] = 0; // first toc entry has to be zero.
449 for (i = 1; i < XING_TOC_SIZE; ++i) {
450 int j = i * mp3->pos / XING_TOC_SIZE;
451 int seek_point = 256LL * mp3->bag[j] / mp3->size;
452 toc[i] = FFMIN(seek_point, 255);
453 }
454
455 /* write replaygain */
456 sd = av_packet_side_data_get(s->streams[0]->codecpar->coded_side_data,
457 s->streams[0]->codecpar->nb_coded_side_data,
459 if (sd && sd->size >= sizeof(*rg)) {
460 uint16_t val;
461
462 rg = (AVReplayGain *)sd->data;
463 AV_WB32(mp3->xing_frame + mp3->xing_offset + 131,
464 av_rescale(rg->track_peak, 1 << 23, 100000));
465
466 if (rg->track_gain != INT32_MIN) {
467 val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1);
468 val |= (rg->track_gain < 0) << 9;
469 val |= 1 << 13;
470 AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val);
471 }
472
473 if (rg->album_gain != INT32_MIN) {
474 val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1);
475 val |= (rg->album_gain < 0) << 9;
476 val |= 1 << 14;
477 AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val);
478 }
479 }
480
481 /* write encoder delay/padding */
482 if (mp3->padding)
483 mp3->padding += 528 + 1;
484 if (mp3->delay >= 1 << 12) {
485 mp3->delay = (1 << 12) - 1;
486 av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
487 }
488 if (mp3->padding >= 1 << 12) {
489 mp3->padding = (1 << 12) - 1;
490 av_log(s, AV_LOG_WARNING, "Too many samples of trailing padding.\n");
491 }
492 AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (mp3->delay << 12) + mp3->padding);
493
494 AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
495 AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
496
497 tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190);
498 AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc);
499
500 avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET);
501 avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
502 avio_seek(s->pb, old_pos, SEEK_SET);
503}
504
506{
507 uint8_t buf[ID3v1_TAG_SIZE];
508 MP3Context *mp3 = s->priv_data;
509 int ret;
510
511 if (mp3->pics_to_write) {
512 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
513 "attached pictures.\n");
514 if ((ret = mp3_queue_flush(s)) < 0)
515 return ret;
516 }
517
518 /* write the id3v1 tag */
519 if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
520 avio_write(s->pb, buf, ID3v1_TAG_SIZE);
521 }
522
523 if (mp3->xing_offset)
525
526 return 0;
527}
528
529static int query_codec(enum AVCodecID id, int std_compliance)
530{
532
533 if (id == AV_CODEC_ID_MP3)
534 return 1;
535
536 while(cm->id != AV_CODEC_ID_NONE) {
537 if(id == cm->id)
538 return MKTAG('A', 'P', 'I', 'C');
539 cm++;
540 }
541 return 0;
542}
543
544static const AVOption options[] = {
545 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
546 offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM},
547 { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
548 offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
549 { "write_xing", "Write the Xing header containing file duration.",
550 offsetof(MP3Context, write_xing), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
551 { NULL },
552};
553
554static const AVClass mp3_muxer_class = {
555 .class_name = "MP3 muxer",
556 .item_name = av_default_item_name,
557 .option = options,
558 .version = LIBAVUTIL_VERSION_INT,
559};
560
562{
563 MP3Context *mp3 = s->priv_data;
564
565 if (pkt->stream_index == mp3->audio_stream_idx) {
566 if (mp3->pics_to_write) {
567 /* buffer audio packets until we get all the pictures */
568 int ret = ff_packet_list_put(&mp3->queue, pkt, NULL, 0);
569
570 if (ret < 0) {
571 av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n");
572 mp3->pics_to_write = 0;
573 if ((ret = mp3_queue_flush(s)) < 0)
574 return ret;
576 }
577 } else
579 } else {
580 AVIOContext *pb = mp3->id3_pb ? mp3->id3_pb : s->pb;
581 int ret;
582
583 /* warn only once for each stream */
584 if (s->streams[pkt->stream_index]->nb_frames == 1) {
585 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
586 " ignoring.\n", pkt->stream_index);
587 }
588 if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
589 return 0;
590
591 ret = ff_id3v2_write_apic(s, pb, &mp3->id3, pkt);
592 if (ret < 0)
593 return ret;
594 if (pb->error < 0)
595 return pb->error;
596 mp3->pics_to_write--;
597
598 /* flush the buffered audio packets */
599 if (!mp3->pics_to_write &&
600 (ret = mp3_queue_flush(s)) < 0)
601 return ret;
602 }
603
604 return 0;
605}
606
607/**
608 * Write an ID3v2 header at beginning of stream
609 */
610
611static int mp3_init(struct AVFormatContext *s)
612{
613 MP3Context *mp3 = s->priv_data;
614 int i;
615
616 if (mp3->id3v2_version &&
617 mp3->id3v2_version != 3 &&
618 mp3->id3v2_version != 4) {
619 av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only "
620 "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version);
621 return AVERROR(EINVAL);
622 }
623
624 /* check the streams -- we want exactly one audio and arbitrary number of
625 * video (attached pictures) */
626 mp3->audio_stream_idx = -1;
627 for (i = 0; i < s->nb_streams; i++) {
628 AVStream *st = s->streams[i];
630 if (mp3->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_MP3) {
631 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
632 "audio stream is required.\n");
633 return AVERROR(EINVAL);
634 }
635 mp3->audio_stream_idx = i;
636 } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
637 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
638 return AVERROR(EINVAL);
639 }
640 }
641 if (mp3->audio_stream_idx < 0) {
642 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
643 return AVERROR(EINVAL);
644 }
645 mp3->pics_to_write = s->nb_streams - 1;
646
647 if (mp3->pics_to_write && !mp3->id3v2_version) {
648 av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the "
649 "ID3v2 header is disabled.\n");
650 return AVERROR(EINVAL);
651 }
652
653 return 0;
654}
655
657{
658 MP3Context *mp3 = s->priv_data;
659 AVIOContext *pb = s->pb;
660 int ret;
661
662 if (mp3->id3v2_version) {
663 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
664 ret = avio_open_dyn_buf(&mp3->id3_pb);
665 if (ret < 0)
666 return ret;
667 pb = mp3->id3_pb;
668 }
669
671 ret = ff_id3v2_write_metadata(s, pb, &mp3->id3);
672 if (ret < 0)
673 return ret;
674 if (pb->error < 0)
675 return pb->error;
676 }
677
678 if (!mp3->pics_to_write) {
679 if (mp3->id3v2_version && (ret = mp3_finish_id3v2(s)) < 0)
680 return ret;
682 }
683
684 return 0;
685}
686
687static void mp3_deinit(struct AVFormatContext *s)
688{
689 MP3Context *mp3 = s->priv_data;
690
693 av_freep(&mp3->xing_frame);
694}
695
697 .p.name = "mp3",
698 .p.long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
699 .p.mime_type = "audio/mpeg",
700 .p.extensions = "mp3",
701 .priv_data_size = sizeof(MP3Context),
702 .p.audio_codec = AV_CODEC_ID_MP3,
703 .p.video_codec = AV_CODEC_ID_PNG,
704 .init = mp3_init,
705 .write_header = mp3_write_header,
706 .write_packet = mp3_write_packet,
707 .write_trailer = mp3_write_trailer,
708 .deinit = mp3_deinit,
709 .query_codec = query_codec,
710 .p.flags = AVFMT_NOTIMESTAMPS,
711 .p.priv_class = &mp3_muxer_class,
712};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFOutputFormat ff_mp3_muxer
Definition mp3enc.c:696
channels
Definition aptx.h:31
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Main libavformat public API header.
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_wb24(AVIOContext *s, unsigned int val)
Definition aviobuf.c:458
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1410
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1365
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1377
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1438
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition aviobuf.c:192
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
static AVPacket * pkt
Public dictionary API.
#define cm
Definition dvbsubdec.c:40
const char * key
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition opt.h:351
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_PNG
Definition codec_id.h:111
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:454
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition packet.c:570
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_REPLAYGAIN
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition packet.h:96
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t AVCRC
Definition crc.h:46
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_16_ANSI_LE
Definition crc.h:54
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition id3v1.c:26
#define ID3v1_TAG_SIZE
Definition id3v1.h:27
#define ID3v1_GENRE_MAX
Definition id3v1.h:29
const CodecMime ff_id3v2_mime_tags[]
Definition id3v2.c:138
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition id3v2enc.c:349
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes)
Finalize an opened ID3v2 tag.
Definition id3v2enc.c:556
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
int ff_id3v2_write_apic(AVFormatContext *s, AVIOContext *pb, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition id3v2enc.c:498
int ff_id3v2_write_metadata(AVFormatContext *s, AVIOContext *pb, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition id3v2enc.c:476
static int query_codec(enum AVCodecID id, int std_compliance)
Definition img2enc.c:361
#define AV_WB24(p, d)
#define AV_WB32(p, v)
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_WB16(p, v)
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
int ff_packet_list_get(PacketList *list, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition packet_list.c:89
void ff_packet_list_free(PacketList *list)
Wipe the list and unref all the packets in it.
int ff_packet_list_put(PacketList *list, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition packet_list.c:40
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawenc.c:31
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
uint32_t tag
Definition movenc.c:2087
static void mp3_update_xing(AVFormatContext *s)
Definition mp3enc.c:430
static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
Definition mp3enc.c:319
static int mp3_queue_flush(AVFormatContext *s)
Definition mp3enc.c:409
static const AVClass mp3_muxer_class
Definition mp3enc.c:554
static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
Definition mp3enc.c:293
#define XING_NUM_BAGS
Definition mp3enc.c:95
static const uint8_t xing_offtbl[2][2]
Definition mp3enc.c:141
static int mp3_init(struct AVFormatContext *s)
Write an ID3v2 header at beginning of stream.
Definition mp3enc.c:611
static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
Definition mp3enc.c:52
static int query_codec(enum AVCodecID id, int std_compliance)
Definition mp3enc.c:529
static int mp3_write_xing(AVFormatContext *s)
Definition mp3enc.c:146
static void mp3_deinit(struct AVFormatContext *s)
Definition mp3enc.c:687
static int mp3_write_header(struct AVFormatContext *s)
Definition mp3enc.c:656
static int id3v1_set_string(AVFormatContext *s, const char *key, uint8_t *buf, int buf_size)
Definition mp3enc.c:42
static int mp3_finish_id3v2(AVFormatContext *s)
Definition mp3enc.c:385
#define XING_SIZE
Definition mp3enc.c:98
static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition mp3enc.c:561
#define XING_TOC_SIZE
Definition mp3enc.c:96
static int mp3_write_trailer(struct AVFormatContext *s)
Definition mp3enc.c:505
mpeg audio declarations for both encoder and decoder.
#define MPA_MONO
Definition mpegaudio.h:49
#define MPA_STEREO
Definition mpegaudio.h:46
mpeg audio layer common tables.
FF_VISIBILITY_PUSH_HIDDEN const uint16_t ff_mpa_bitrate_tab[2][3][15]
const uint16_t ff_mpa_freq_tab[3]
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
MPEG Audio header decoder.
AVOptions.
static const uint8_t header[24]
Definition sdr2.c:68
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int error
contains the error code or 0 if no error happened
Definition avio.h:239
AVOption.
Definition opt.h:428
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:424
uint8_t * data
Definition packet.h:425
This structure stores compressed data.
Definition packet.h:580
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1....
Definition replaygain.h:29
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition replaygain.h:39
int32_t track_gain
Track replay gain in microbels (divide by 100000 to get the value in dB).
Definition replaygain.h:34
int32_t album_gain
Same as track_gain, but for the whole album.
Definition replaygain.h:43
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition internal.h:111
ID3v2EncContext id3
Definition mp3enc.c:102
uint32_t want
Definition mp3enc.c:123
int pics_to_write
Definition mp3enc.c:135
uint64_t bag[XING_NUM_BAGS]
Definition mp3enc.c:126
int xing_frame_size
Definition mp3enc.c:111
int initial_bitrate
Definition mp3enc.c:127
uint32_t audio_size
Definition mp3enc.c:114
int64_t padding
Definition mp3enc.c:130
AVIOContext * id3_pb
Definition mp3enc.c:103
AVCRC audio_crc
Definition mp3enc.c:113
int id3v2_version
Definition mp3enc.c:104
int audio_stream_idx
Definition mp3enc.c:133
PacketList queue
Definition mp3enc.c:138
int write_xing
Definition mp3enc.c:106
int xing_offset
Definition mp3enc.c:119
int32_t frames
Definition mp3enc.c:121
uint8_t * xing_frame
Definition mp3enc.c:110
int write_id3v1
Definition mp3enc.c:105
int64_t xing_frame_offset
Definition mp3enc.c:117
int has_variable_bitrate
Definition mp3enc.c:128
int32_t size
Definition mp3enc.c:122
uint32_t pos
Definition mp3enc.c:125
int delay
Definition mp3enc.c:129
uint32_t seen
Definition mp3enc.c:124
PacketListEntry * head
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
int size
uint8_t base
Definition vp3data.h:128