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